我如何从 Swift 中的 anyObject 中提取数据

How can i extract data from an anyObject in Swift

我正在使用 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) 转储是...

    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
)

我的问题是,有没有更好的方法来获取这些数据? 如果没有,我如何将 (data, data) anyObject 转换为 [data, data] 数组?

最好的猜测是 TweetsNotLoaded 是 NSNumber 的 NSArray(或者可能是 NSString,不确定它们是如何 coding/storing 消息 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
    ...
}