2016-07-18 8 views
3

Xcodeの8(ベータ1)とスウィフト3にアップグレードしたので、私は、この行でエラーを持っている:型 '(Bool、NSError!) - > Void'の値を期待される引数型 'ACAccountStoreRequestAccessCompletionHandler!'に変換できません。

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

それは言う:

型の値を変換できません「(ブール値、NSError!) - >期待値の型に 'Void' 'ACAccountStoreRequestAccessCompletionHandler!'

は、その行の前に、私は、 "アカウント" と "accountType" を定義した:

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

この私のコード(Xcodeの7とスウィフト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

'ERROR'パラメータではなく、オプションの暗黙的に開封されたのオプションに変更しました。

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

それを 'NSError? 'に変更するか、明示的な型を削除するだけです。とにかくそれらは不要です。 – dan

+1

@danありがとう!それを答えとして追加してください: – Devhess

+0

私の場合は、それをエラーに変更したときに動作しますか? 。とにかくあなたの質問は私を助けてくれてありがとう:) – Mariam

答えて

2

あなたがすべきコードを次のように更新します。

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

このバージョンはXco用です。 de 8 GM Swift3(正規版)

1

xcode 8.2とswift 3では、この方法をチェックしました。 BoolとNSErrorを削除して、成功とエラーを通知します。これはokeyになります。私は幸運、それはあなたを助けることを願っています:)

関連する問題