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

时间:2016-07-18 13:47:38

标签: ios swift xcode social-networking acaccount

自从在Xcode 8(Beta 1)和Swift 3上升级以来,我在这一行中出错:

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

它说:

  

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

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

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

这是我的(使用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")
                                                }
    })

}

2 个答案:

答案 0 :(得分:2)

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

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

此版本适用于Xcode 8 GM Swift3(常规版)

答案 1 :(得分:1)

在xcode 8.2和swift 3中,我检查了这个方法。 删除成功和错误的Bool和NSError,它将是okey。

<a href="javascript:selectRadioButton('idFirstRadioButton')"><img src="http://urltoimage/image.jpg" border="0"></a>

我希望它会对你有所帮助,祝你好运:)