Twitter搜索Api 1.1错误:无法将“__NSCFDictionary”类型的值转换为“NSArray”

时间:2015-09-17 23:31:00

标签: ios swift xcode6 twitter-oauth

此代码用于在Twitter上搜索主题标签,但它提示错误 无法将“__NSCFDictionary”类型的值转换为“NSArray”

当我使用以下URL访问我的家庭时间轴时,相同的代码工作正常 https://api.twitter.com/1.1/statuses/home_timeline.json

func getTimeLine() {

    let accountType = account.accountTypeWithAccountTypeIdentifier(
        ACAccountTypeIdentifierTwitter)

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

            if success {
                let arrayOfAccounts =
                self.account.accountsWithAccountType(accountType)

                if arrayOfAccounts.count > 0 {

                    let twitterAccount = arrayOfAccounts.last as! ACAccount

                    let requestURL = NSURL(string:
                        "https://api.twitter.com/1.1/search/tweets.json?q=%23baseball&result_type=recent%3F")
                      let parameters = ["count" : "10"]

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

                    postRequest.account = twitterAccount

                    postRequest.performRequestWithHandler(
                        {(responseData: NSData!, urlResponse: NSHTTPURLResponse!, error: NSError!) -> Void in
                            var err: NSError?
                            self.array = NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.MutableLeaves, error: &err) as! [AnyObject**strong text**]
                            if self.array.count != 0 {
                                dispatch_async(dispatch_get_main_queue()) {
                                    self.mycollectionView.reloadData()
                                }
                            }
                    })
                }
            }
            else {
                println("Failed to access account")
            }
    })
}

1 个答案:

答案 0 :(得分:3)

尝试向NSDictionary投射。它应该可以解决你的问题。

self.array = NSJSONSerialization.JSONObjectWithData(
    responseData,
    options: NSJSONReadingOptions.MutableLeaves, error: &err
) as! NSDictionary

也许这是来自Twitter的正确回报。