Skip to main content
 首页 » 数据库

ios之使用 Swifter Framework 快速检索 Twitter 用户图像和名称

2024年01月01日84tuyile006

我正在使用 Swifter Framework使用特定主题标签获取推文,但我还需要用户图像和名称。

通过这种方法,我可以获取推文文本,并且可以从 JSON 响应中读取个人资料图片和用户名,但无法将其放入变量中。

PS:不要介意索引 0...19 它只是测试

        let swifter = Swifter(consumerKey: "MyConsumerKey", consumerSecret: "MyConsumerSecret", appOnly: true) 
 
        swifter.authorizeAppOnlyWithSuccess({ (accessToken, response) -> Void in 
        swifter.getSearchTweetsWithQuery("%23realmadrid", geocode: nil, lang: nil, locale: nil, resultType: nil, count: 20, until: nil, sinceID: nil, maxID: nil, includeEntities: true, callback: nil, success: { (statuses, searchMetadata) -> Void in 
 
 
            for index in 0...19 { 
                if let statusText = statuses?[index]["text"].string { 
                    self.tweetsArray.addObject(statusText) 
                } 
                if let statusName = statuses?[index]["screen_name"].string { 
                    println("@%@", statusName) 
                } 
                if let statusImage = statuses?[index]["profile_image_url"].string { 
                    println("@%@", statusImage) 
                } 
 
            } 
 
            self.tableView.reloadData() 
 
 
        }, failure: { (error) -> Void in 
        }) 
    }, failure: { (error) -> Void in 
        println("error") 
    }) 

请您参考如下方法:

遇到同样问题的 friend :

  if let statusName = statuses?[index]["user"]["screen_name"].string { 
                    tweetsDictionary.setObject(statusName, forKey: "name") 
                } 
 
  if let statusImage = statuses?[index]["user"]["profile_image_url"].string { 
                    var string = statusImage.stringByReplacingOccurrencesOfString("_normal", withString: "", options: NSStringCompareOptions.BackwardsSearch, range: nil) 
                    tweetsDictionary.setObject(string, forKey: "image") 
                } 

这会以用户上传的质量检索 Twitter 用户名和个人资料图片。