我正在使用TwitterKit SDK并列出一组推文。该函数有一个错误处理程序,用于存储已被用户删除但未显示的任何推文。我试图从NSError用户信息字典中检索这些特定的ID。我可以找到它们但最终得到一个anyObject。
此代码获取推文对象并过滤掉不良内容......
// load tweets with guest login
Twitter.sharedInstance().logInGuestWithCompletion {
(session: TWTRGuestSession!, error: NSError!) in
// Find the tweets with the tweetIDs
Twitter.sharedInstance().APIClient.loadTweetsWithIDs(tweetIDs) {
(twttrs, error) - > Void in
// If there are tweets do something magical
if ((twttrs) != nil) {
// Loop through tweets and do something
for i in twttrs {
// Append the Tweet to the Tweets to display in the table view.
self.tweetsArray.append(i as TWTRTweet)
}
} else {
println(error)
}
println(error)
if let fails: AnyObject = error.userInfo?["TweetsNotLoaded"] {
println(fails)
}
}
}
println(错误)转储是......
Error Domain=TWTRErrorDomain Code=4 "Failed to fetch one or more of the following tweet IDs: 480705559465713666, 489783592151965697." UserInfo=0x8051ab80 {TweetsNotLoaded=(
480705559465713666,
489783592151965697
), NSLocalizedDescription=Failed to fetch one or more of the following tweet IDs: 480705559465713666, 489783592151965697.}
并改进错误结果“error.userInfo?[”TweetsNotLoaded“]”我最终可以......
(
480705559465713666,
489783592151965697
)
我的问题是,有没有更好的方法来获取这些数据? 如果没有,我怎样才能将(数据,数据)anyObject转换为[数据,数据]数组?
答案 0 :(得分:1)
最好的猜测是TweetsNotLoaded是NSNumber的NSArray(或者NSString,不知道他们如何编码/存储消息ID),所以你可以投射结果并从那里开始:
if let tweetsNotLoaded = error.userInfo?["TweetsNotLoaded"] as? [String] {
// here tweets not loaded will be [String], deal with them however
...
}
如果这不起作用,假设他们长期使用:
if let tweetsNotLoaded = error.userInfo?["TweetsNotLoaded"] as? [Int64] {
// here tweets not loaded will be [Int64], deal with them however
...
}