xcode 无法将 '(Bool, NSError!) -> Void' 类型的值转换为预期的参数类型 'ACAccountStoreRequestAccessCompletionHandler!'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38438418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:03:19  来源:igfitidea点击:

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

iosswiftxcodesocial-networkingacaccount

提问by Devhess

Since upgrading on Xcode 8 (Beta 1) and Swift 3 I have an error in this line:

自从在 Xcode 8 (Beta 1) 和 Swift 3 上升级后,我在这一行出现错误:

account.requestAccessToAccounts(with: accountType, options: nil, completion: {(success: Bool, error: NSError!) -> Void in

It says:

它说:

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

无法将 '(Bool, NSError!) -> Void' 类型的值转换为预期的参数类型 'ACAccountStoreRequestAccessCompletionHandler!'

Before that line, I defined "account" and "accountType":

在该行之前,我定义了“account”和“accountType”:

let account = ACAccountStore()
let accountType = account.accountType(
        withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter)

This is my (with Xcode 7 and Swift 2 working) code:

这是我的(使用 Xcode 7 和 Swift 2 工作)代码:

func getTimeline() {

    //https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2
    let account = ACAccountStore()
    let accountType = account.accountType(
        withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter)

    account.requestAccessToAccounts(with: accountType, options: nil,
                                            completion: {(success: Bool, error: NSError!) -> Void in

                                                if success {
                                                    let arrayOfAccounts =
                                                        account.accounts(with: accountType)

                                                    if arrayOfAccounts.count > 0 {
                                                        let twitterAccount = arrayOfAccounts.last as! ACAccount

                                                        let requestURL = URL(string:
                                                            "https://api.twitter.com/1.1/statuses/user_timeline.json")

                                                        let parameters = ["screen_name": self.userName!,
                                                            "count" : "20"]

                                                        let postRequest = SLRequest(forServiceType:
                                                            SLServiceTypeTwitter,
                                                            requestMethod: SLRequestMethod.GET,
                                                            url: requestURL,
                                                            parameters: parameters)

                                                        postRequest.account = twitterAccount

                                                        postRequest.perform(
                                                            handler: {(responseData: Data!,
                                                                urlResponse: HTTPURLResponse!,
                                                                error: NSError!) -> Void in

                                                                if error != nil {

                                                                    Crashlytics.sharedInstance().recordError(error)

                                                                }
                                                                do {
                                                                    self.dataSource = try JSONSerialization.jsonObject(with: responseData, options: JSONSerialization.ReadingOptions.mutableLeaves) as! [AnyObject]

                                                                    if self.dataSource.count != 0 {
                                                                        DispatchQueue.main.async {
                                                                            self.tableView.reloadData()
                                                                        }
                                                                    }
                                                                } catch {
                                                                    print("catching")
                                                                }
                                                        })
                                                    }
                                                } else {
                                                    print("Failed to access account")
                                                }
    })

}

回答by Stéphane de Luca

You should update your code as follows:

您应该按如下方式更新您的代码:

    account.requestAccessToAccounts(with: accountType, options: [:]) {
        (success: Bool, error: Error?) -> Void in
        // blah blah: the rest of the code
    }            

This version is for Xcode 8 GM Swift3 (regular version)

此版本适用于 Xcode 8 GM Swift3(普通版)

回答by ShervinJam

In xcode 8.2 and swift 3 , I checked this method. Remove Bool and NSError infront of success and error and it will be okey.

在 xcode 8.2 和 swift 3 中,我检查了这个方法。删除成功和错误前面的 Bool 和 NSError 就可以了。

account.requestAccessToAccounts(with: accountType, options: nil,
                                        completion: {(success, error) -> Void in

I hope it will help you, Good Luck :)

希望能帮到你,祝你好运:)