加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 无法将类型'(Bool,NSError!) – > Void’的值转

发布时间:2020-12-14 17:35:39 所属栏目:百科 来源:网络整理
导读:自从在 Xcode 8(Beta 1)和 Swift 3上升级后,我在此行中出错: account.requestAccessToAccounts(with: accountType,options: nil,completion: {(success: Bool,error: NSError!) - Void in 它说: Cannot convert value of type ‘(Bool,NSError!) - Void’
自从在 Xcode 8(Beta 1)和 Swift 3上升级后,我在此行中出错:

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

它说:

Cannot convert value of type ‘(Bool,NSError!) -> Void’ to expected argument type ‘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,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")
                                                }
    })

}

解决方法

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读