简体   繁体   中英

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

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

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!'

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

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

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

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

}

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)

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.

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

I hope it will help you, Good Luck :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM