R – MGTwitterEngine and iPhone

iphonemgtwitterengineobjective ctwitter

I downloaded MGTwitterEngine and added to my iPhone project. It's connecting and getting statues I can tell from dumping them into an NSLog. But, I can't figure out how how I need to parse the calls so I can add them to a table. They are returned as an NSString and look like this:

      {
    "created_at" = 2009-07-25 15:28:41 -0500;
    favorited = 0;
    id = 65;
    source = "<a href=\"http://twitter.com/\">Twitter</a>";
    "source_api_request_type" = 0;
    text = "The wolf shirt strikes again!! #sdcc :P http://twitpic.com/blz4b";
    truncated = 0;
    user =         {
        "created_at" = "Sat Jul 25 20:34:33 +0000 2009";
        description = "Host of Tekzilla on Revision3 and Qore on PSN. Also, a geek.";
        "favourites_count" = 0;
        "followers_count" = 0;
        following = false;
        "friends_count" = 0;
        id = 5;
        location = "San Francisco";
        name = "Veronica Belmont";
        notifications = false;
        "profile_background_tile" = false;
        "profile_image_url" = "http://blabnow.com/avatar/Twitter_10350_new_twitter_normal.jpg";
        protected = 0;
        "screen_name" = Veronica;
        "statuses_count" = 2;
        "time_zone" = UTC;
        url = "http://www.veronicabelmont.com";
        "utc_offset" = 0;
    };

Anybody used this that can tell me how everyone else uses it in their project?

Thanks

Best Answer

What you are seeing in your console is an NSLog of an NSDictionary and not an NSString. From Matt Gemmell's MGTwitterEngine Readme:

The values sent to these methods are all NSArrays containing an NSDictionary for each status or user or direct message, with sub-dictionaries if necessary (for example, the timeline methods usually return statuses, each of which has a sub-dictionary giving information about the user who posted that status).

So whatever object you passed to your NSLog() statement is actually a dictionary and you can access the fields with a call to:

NSString *createdAtDate = [record valueForKey:@"created_at"];
NSString *source = [record valueForKey:@"source"];
// etc...

Where record is the object. Keep in mind that the user field is a sub-dictionary. You access it this way:

NSDictionary *userDict = [record valueForKey:@"user"];
NSString *name = [userDict valueForKey:@"name"];
NSString *location = [userDict valueForKey:@"location"];
// etc...

You could actually use the NSArray returned in the request as your table view's data source and then just extract the one you need by the index in your -cellForRowAtIndexPath table view delegate.

Best Regards,

Related Topic