[已解决]swift Type () does not conform to protocol BooleanType

代码:

            do{
                let isConnectOk = try stream.connectWithTimeout(XMPPStreamTimeoutNone)

                if isConnectOk
                {
                    var alert = UIAlertController(title: "Alert", message: "Cannot connect to : \(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.Alert)

                    alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
                    self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)

                    return false
                }

            }catch{
                print(error)
            }

出错:

SwiftXMPP/AppDelegate.swift:117:20: Type ‘()’ does not conform to protocol ‘BooleanType’

Type does not conform to protocol BooleanType

 

参考:

最后改为:
            do{
                try stream.connectWithTimeout(XMPPStreamTimeoutNone)
            }catch{
                let alert = UIAlertController(title: "Alert", message: "Cannot connect to : \(error)", preferredStyle: UIAlertControllerStyle.Alert)

                alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
                self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)

                return false
            }
才通过编译了。
感觉像是:
ObjC的代码:
- (BOOL)connectWithTimeout:(NSTimeInterval)timeout error:(NSError **)errPtr;
原先用法是:
            if !stream.connectWithTimeout(XMPPStreamTimeoutNone, error: &error) {
                var alert = UIAlertController(title: "Alert", message: "Cannot connect to : \(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.Alert)

                alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
                self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)

                return false
            }
现在改为:
            do{
                try stream.connectWithTimeout(XMPPStreamTimeoutNone)
            }catch{
                let alert = UIAlertController(title: "Alert", message: "Cannot connect to : \(error)", preferredStyle: UIAlertControllerStyle.Alert)

                alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
                self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)

                return false
            }
才可以。


发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量