【问题标题】:Twitter Search Api 1.1 ERROR: Could not cast value of type '__NSCFDictionary' to 'NSArray'Twitter Search Api 1.1 错误:无法将“__NSCFDictionary”类型的值转换为“NSArray”
【发布时间】:2015-09-17 23:31:00
【问题描述】:

此代码是在 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")
            }
    })
}

【问题讨论】:

    标签: ios swift xcode6 twitter-oauth


    【解决方案1】:

    尝试对NSDictionary 进行强制转换。它应该可以解决您的问题。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      • 2020-03-26
      相关资源
      最近更新 更多