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

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

问题描述

我在 Xcode 7.3 中有一个 swift 2.2 版本的工作项目.现在我已经更新了 Xcode 8 并迁移到了 swift 3.现在我的项目包含专门针对成功块等块的错误.

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.

哪个给出错误

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

我不明白如何按照 swift 3 解决这个问题.

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

Facebook 登录也有类似的错误.

And there is also same like error in Facebook login.

哪个给出错误

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

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

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

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.

推荐答案

虽然我之前不知道 Xcode 想要通知我有关错误的错误,但我已经删除了对象的类型规范并且它起作用了.

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.

作为

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)
                    }


                }

        })

这里关于问题错误在 dataTaskresponseObj 中给出,它们具有指定的类型.删除类型后它工作正常.

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

与facebook登录相同

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()
                }
            }
        })

    }

这里我也删除了 resulterror 的类型规范,问题解决了.并在整个应用程序中遵循这一点,它奏效了.我可以毫无错误地运行该项目,并且它正在运行.谢谢.

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天全站免登陆