views:

2056

answers:

4

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/\"&gt;Twitter&lt;/a&gt;";
    "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

+7  A: 

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,

Matt Long
Thanks Matt. Great answer. Can you give me an example of what you meant by using the NSArray as the tables data source and extracting what I need?
Xcoder
A: 

When you added the MGTwitterEngine to your project, what did you have to do to get it to compile? I get tons of errors.

Troy Sartain
A: 

Check this for Compiling MGTwitterEngine for the iPhone using Xcode

link text

raaz
A: 

For anyone else who might find their way here, here's one way to parce the results. (From a newbie, so don't count of this being the standard or even correct way)

The key (pun intended :D) is to use the dictionary in the appropriate delegate method. Check out Matt Long's example code in another thread on the topic.

To parce something like this:

[myTwitterEngine getSearchResultsForQuery:@"#ironsavior"];

His example is this, in the delegate method:

- (void)searchResultsReceived:(NSArray *)searchResults 
                   forRequest:(NSString *)connectionIdentifier
{
    if ([searchResults count] > 0)
    {
        NSDictionary *result = [searchResults objectAtIndex:0];

        NSString *fromUser = [result valueForKey:@"from_user"];
        NSString *fromUserID = [result valueForKey@"from_user_id"];
        // ...
        NSString *text = [result valueForKey@"text"];

        NSLog(@"User %@(%@): %@", fromUser, fromUserID, text);
    }
}

This would give you a very simple message that goes "User username(userid): message".

I'm not sure what the best way to proceed would be, I'm thinking returning a dictionary or an array that you could use elsewhere in your implementation. Or just return the original array and parse it elsewhere.

Check out that other thread for more info.

Parimal