为啥func调用中的代码块没有被执行?

Posted

技术标签:

【中文标题】为啥func调用中的代码块没有被执行?【英文标题】:why is the block of code inside func call is not executed?为什么func调用中的代码块没有被执行? 【发布时间】:2016-07-10 06:07:56 【问题描述】:

我正在尝试在我的应用程序中实现一个 Xmpp 客户端。我计划在我的应用程序中使用以下代码,但部分代码似乎没有执行,这会阻止我连接到服务器。 https://github.com/processone/xmpp-messenger-ios

我正在尝试使用我的 jabberID 和密码连接到服务器,但对于某些人来说,此代码永远不会执行:

if let _ = error 
     let alertController = UIAlertController(title: "Sorry", message: "An error occured: \(error)", preferredStyle: UIAlertControllerStyle.Alert)
         alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:  (UIAlertAction) -> Void in
         //do smt
     ))
        self.presentViewController(alertController, animated: true, completion: nil)
      else 
        self.dismissViewControllerAnimated(true, completion: nil)
     

这是与问题相关的整个代码:

    @IBAction func validate(sender: AnyObject) 
            if OneChat.sharedInstance.isConnected() 
                OneChat.sharedInstance.disconnect()
                usernameTextField.hidden = false
                passwordTextField.hidden = false
                validateButton.setTitle("Validate", forState: UIControlState.Normal)
             else 
                OneChat.sharedInstance.connect(username: self.usernameTextField.text!, password: self.passwordTextField.text!) (stream, error) -> Void in
                    if let _ = error 
                        let alertController = UIAlertController(title: "Sorry", message: "An error occured: \(error)", preferredStyle: UIAlertControllerStyle.Alert)
                        alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:  (UIAlertAction) -> Void in
                            //do smt
                        ))
                        self.presentViewController(alertController, animated: true, completion: nil)
                     else 
                        self.dismissViewControllerAnimated(true, completion: nil)
                    
                
            
        

public typealias OneChatConnectCompletionHandler = (stream: XMPPStream, error: DDXMLElement?) -> Void

public func connect(username username: String, password: String, completionHandler completion:OneChatConnectCompletionHandler) 
        if isConnected() 
            streamDidConnectCompletionBlock = completion
            self.streamDidConnectCompletionBlock!(stream: self.xmppStream!, error: nil)
            return
        

        if (username == "kXMPPmyJID" && NSUserDefaults.standardUserDefaults().stringForKey(kXMPP.myJID) == "kXMPPmyJID") || (username == "kXMPPmyJID" && NSUserDefaults.standardUserDefaults().stringForKey(kXMPP.myJID) == nil) 
            streamDidConnectCompletionBlock = completion
            streamDidConnectCompletionBlock!(stream: self.xmppStream!, error: DDXMLElement(name: "Please set crendentials before trying to connect"))
            return
        

        if username != "kXMPPmyJID" 
            setValue(username, forKey: kXMPP.myJID)
            setValue(password, forKey: kXMPP.myPassword)
        

        if let jid = NSUserDefaults.standardUserDefaults().stringForKey(kXMPP.myJID) 
            xmppStream?.myJID = XMPPJID.jidWithString(jid)
         else 
            streamDidConnectCompletionBlock = completion //was false
            streamDidConnectCompletionBlock!(stream: self.xmppStream!, error: DDXMLElement(name: "Bad username"))
        

        if let password = NSUserDefaults.standardUserDefaults().stringForKey(kXMPP.myPassword) 
            self.password = password
         else 
            streamDidConnectCompletionBlock = completion //was false
            streamDidConnectCompletionBlock!(stream: self.xmppStream!, error: DDXMLElement(name: "Bad password"))
        
        try! xmppStream!.connectWithTimeout(XMPPStreamTimeoutNone)

        streamDidConnectCompletionBlock = completion
    

【问题讨论】:

您的代码的哪一部分没有被执行。 正如我在上面指出的以 if let _ = error 开头的代码 你得到的错误值是多少?或者你的响应块没有被调用? 我没有收到任何错误,但据我从代码中了解, 如果错误值为 nil 那么它应该执行 else 部分。尝试明确检查错误!=nil 【参考方案1】:

也许,您期望永远不会发生超时(库的配置) 我有同样的问题,最后,我发现我的问题是主机名和端口。您是否尝试过使用这样的东西:

let usuario = "name@domain.com";

OneChat.sharedInstance.connect(username: usuario, password: pass)  (stream, error) -> Void in


        if let _ = error 
            let alertController = UIAlertController(title: "Sorry", message: "An error occured: \(error)", preferredStyle: UIAlertControllerStyle.Alert)
            alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:  (UIAlertAction) -> Void in
                //do something
            ))
            self.presentViewController(alertController, animated: true, completion: nil)
         else 
            print("I did it!")

        
    

【讨论】:

以上是关于为啥func调用中的代码块没有被执行?的主要内容,如果未能解决你的问题,请参考以下文章

两个案例,解读静态代码块和构造代码块

ruby中的作用域和代码块

java-普通代码块构造代码块和静态代码块的区别。

Java中静态代码块构造代码块的区别

为啥函数被描述为块范围

08-02 Java 代码块,代码块执行的先后顺序问题