简体   繁体   English

如何在Swift中从AnyObject提取数据

[英]How can i extract data from an anyObject in Swift

I'm using the TwitterKit SDK and listing a group of Tweets. 我正在使用TwitterKit SDK并列出一组Tweets。 The function has an error handler that stores any tweets that have been removed by the user and thus not shown. 该函数具有一个错误处理程序,该错误处理程序存储了用户已删除并因此未显示的所有推文。 I am attempting to retrieve these particular ID's from the NSError user info dictionary. 我正在尝试从NSError用户信息字典中检索这些特定的ID。 I can find them but end up with an anyObject. 我可以找到它们,但最终得到一个anyObject。

This code is getting the tweet objects and filtering out the bad ones... 这段代码正在获取tweet对象并过滤出不良对象...

             // 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)
            }
          }
        }

The println(error) dump is... 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.}

and refining the results from the error "error.userInfo?["TweetsNotLoaded"]" I can end up with... 并完善错误“ error.userInfo?[“ TweetsNotLoaded”]“的结果,我最终可以...

    (
    480705559465713666,
    489783592151965697
)

My question is, is there a better way to get this data? 我的问题是,是否有更好的方法来获取此数据? If not, how am I able to convert the (data, data) anyObject into an array of [data, data] ? 如果不是,我如何将(data,data)anyObject转换为[data,data]数组?

Best guess is that TweetsNotLoaded is an NSArray of NSNumber (or maybe NSString, not sure how they're coding/storing message ids), so you can cast the result and go from there: 最好的猜测是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 that doesn't work, assume they're longs and use: 如果那不起作用,请假设它们很长,可以使用:

if let tweetsNotLoaded = error.userInfo?["TweetsNotLoaded"] as? [Int64] {
    // here tweets not loaded will be [Int64], deal with them however
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM