完成处理程序在swift 3和Xcode 8中的错误 [英] completion handler's error in swift 3 and Xcode 8

查看:200
本文介绍了完成处理程序在swift 3和Xcode 8中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xcode 7.3中使用swift 2.2版本的工作项目。现在我已经更新了Xcode 8并迁移到swift 3.现在我的项目包含了非常成功阻止网络的块的错误。





哪些错误作为

 无法转换类型的值) - > ()'到期望的参数类型'((URLSessionDataTask,Any?) - > Void)?

我不明白如何解决这个工作,根据swift 3。



而且在Facebook登录中也有同样的错误。





哪些错误为

 无法转换类型的值(FBSDKLoginManagerLoginResult !, NSError!) - > Void'到期望的参数类型'FBSDKLoginManagerRequestTokenHandler!'

 不能转换'(_,_,NSError!)'类型的值 - > Void'到期望的参数类型'FBSDKGraphRequestHandler!'

这个所有错误都与swift 3中的处理程序块有关我不明白错误,所以无法解决。任何帮助将不胜感激。感谢提前。

解决方案

虽然我不知道错误之前,Xcode想通知我关于错误,但是我已经删除了对象的类型规范,它的工作。



As

  manager.post(methodname,parameters:param,progress:nil,success:{(dataTask,responseObj)in 

if let dict:NSDictionary = responseObj as?NSDictionary {

print(\(methodname)的响应:\(dict))

如果dict.object(forKey:response)as?String ==success{
CompletionHandler(true,dict)
} else {
CompletionHandler(false,dict)
}


}

})

这里关于问题错误在 dataTask responseObj 与指定的类型。删除后,它工作正常。



与facebook登录相同

  @IBAction func fbLoginClicked(_发件人:AnyObject){

let app = UIApplication.shared.delegate as! AppDelegate

app.fbLoginManager = FBSDKLoginManager()
app.fbLoginManager.logOut()
app.fbLoginManager.loginBehavior = FBSDKLoginBehavior.native


app.fbLoginManager.logIn(withReadPermissions:[email],from:self,handler:{(result,error) - > Void in
if error!= nil {
print ?.localizedDescription)
} else {

if(result!as FBSDKLoginManagerLoginResult).isCancelled == true {

} else {

self.fetchFacebookUserDetail()
}
}
})

}

这里我也删除了 result 错误的问题解决的类型规范。并在整个应用程序中遵循这一点,它的工作。我可以运行该项目没有错误,它也正在工作。谢谢。


I have working project in Xcode 7.3 with swift 2.2 version. Now I have updated Xcode 8 and migrated to swift 3. Now my project contains errors specially for blocks like success block of afnetworking.

Which gives error as

Cannot convert value of type '() -> ()' to expected argument type '((URLSessionDataTask, Any?) -> Void)?'

I don't understand how to solve this to work as per swift 3.

And there is also same like error in Facebook login.

Which gives error as

Cannot convert value of type '(FBSDKLoginManagerLoginResult!, NSError!) -> Void' to expected argument type 'FBSDKLoginManagerRequestTokenHandler!'

and

Cannot convert value of type '(_, _, NSError!) -> Void' to expected argument type 'FBSDKGraphRequestHandler!'

This all errors are related to handler blocks in swift 3. I don't understand the errors and so that can't able to solve. Any help will be appreciated. Thanks in advance.

解决方案

Though I didn't know the error before that what Xcode want to inform me about the error, but I have removed type specification with object and it worked.

As

manager.post(methodname, parameters: param, progress: nil, success:{ (dataTask, responseObj) in

                if let dict : NSDictionary = responseObj as? NSDictionary {

                    print("Response of \(methodname) : \(dict)")

                    if dict.object(forKey: "response") as? String == "success" {
                        CompletionHandler(true, dict)
                    } else {
                        CompletionHandler(false, dict)
                    }


                }

        })

Here with respect to question error is given at dataTask and responseObj which are with type specified. After removing type it worked fine.

Same as with facebook login

@IBAction func fbLoginClicked(_ sender: AnyObject) {

        let app = UIApplication.shared.delegate as! AppDelegate

        app.fbLoginManager = FBSDKLoginManager()
        app.fbLoginManager.logOut()
        app.fbLoginManager.loginBehavior = FBSDKLoginBehavior.native


        app.fbLoginManager.logIn(withReadPermissions: ["email"], from: self, handler: { (result, error) -> Void in
            if error != nil {
                print(error?.localizedDescription)
            } else {

                if (result! as FBSDKLoginManagerLoginResult).isCancelled == true {

                } else {

                    self.fetchFacebookUserDetail()
                }
            }
        })

    }

Here also I have removed type specification of result and error and problem solved. And followed this in whole app and it worked. I can run the project without error and also it is working. Thanks.

这篇关于完成处理程序在swift 3和Xcode 8中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆