user482024
user482024

Reputation: 95

How can i extract data from an anyObject in Swift

I'm using the TwitterKit SDK and listing a group of 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. I can find them but end up with an anyObject.

This code is getting the tweet objects and filtering out the bad ones...

             // 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...

    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...

    (
    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] ?

Upvotes: 0

Views: 572

Answers (1)

David Berry
David Berry

Reputation: 41226

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:

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
    ...
}

Upvotes: 1

Related Questions