Greg’s Online CS 193P Resources

May 6, 2009

Adding reading the .plist to Presence #2

Filed under: cs 193p — gregorykaiser @ 3:35 pm
Tags:

When I start each assignment I find it takes an hour or two of searching and reading documents to figure out the basics of the new concepts to get some sample code going.

I have now added reading the TwitterUsers.plist to the JSON code from May 5th. I think this will help you get going. It is my -viewDidLoad code which reads the plist and goes to Twitter. Output is via NSLOG.

Also note that the constant setting to send requests to the real twitter.com is in TwitterHelper.h (#define USE_TEST_SERVER 1) and you can change this to “0” instead of changing the #if stuf in ViewController.m

(this is an update to the “Getting JSON and TwitterHelper Working” post from May 5)

- (void)viewDidLoad {
	[super viewDidLoad];  // moved to start see comment 4
	//P2ap start
	// TTD - Must figure out variable allocation and deallocation
	//get the plist information
	NSString *filePath = [[NSBundle mainBundle] pathForResource:@"TwitterUsers" ofType:@"plist"];  
	if (filePath) {  
		NSArray *twitterIds = [NSArray arrayWithContentsOfFile:filePath];  
		// NSLog(@"TwitterUser.plist=%@", twitterIds);

		NSString *displayname = @"None";
		NSString *picURL = @"Nonepic";  
		if (twitterIds != nil) {
			for (int count = 0; count < &#91;twitterIds count&#93;; count++) {
				NSLog(@"======> TwitterID %d:%@", count, [twitterIds objectAtIndex:count]);
				
				// now get information from twitter
				NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:[TwitterHelper  fetchInfoForUsername:[twitterIds objectAtIndex:count]]];
				displayname = [dict objectForKey:@"name"];
				NSLog(@"======>Twitter Display Name for %@ - %@", [twitterIds objectAtIndex:count], displayname);
				picURL = [dict objectForKey:@"profile_image_url"];
				NSLog(@"======>Twitter Picture URL for %@ - %@", [twitterIds objectAtIndex:count], picURL);
				NSArray *userTimeline = [[NSArray alloc] initWithArray:[TwitterHelper  fetchTimelineForUsername:[twitterIds objectAtIndex:count]]]; 
				// Uncomment the next line to see the entire array of User Timeline (its big)
				//	NSLog(@"======>Twitter Timeline for fr8:%@", userTimeline);
				for ( int i = 0 ; i< &#91;userTimeline count&#93;; i++) {
					NSLog(@"==============>Timeline  Array at index %d Status Msg:%@", i, [[userTimeline objectAtIndex: i] objectForKey:@"text" ]);
				}
				
				[ displayname release];
				[ picURL release];				
				
			}
		}
	}  
	//P2At end
	// [super viewDidLoad];  //moved to beginning see comment 4
}
Advertisements

4 Comments »

  1. I’m curious about why you use InitWithDictionary: and initWithArray for the JSON results. You can just assign and retain the results like this:

    person.twitterInfoForUser = [TwitterHelper fetchInfoForUsername:name];

    or

    tweetList = [[TwitterHelper fetchTimelineForUsername:[aPerson.twitterInfoForUser objectForKey:@”screen_name”]] retain];

    Another thing you might like to look at is Fast Enumeration for your stepping through the NSDictionary/NSArray:

    for (NSString *name in twitterIds)
    {
    // get info
    }

    I don’t know too much about this either, but these seem more efficient. Email me if you want to discuss code together.

    Comment by Jeff — May 7, 2009 @ 12:00 pm | Reply

    • Jeff,
      Thanks for the comments and I agree on both points. Being new to all of the iPhone dev stuff I have to build up my code by figuring each step out. Here I was concentrating on plists and Twitterhelper and I did not have a shared person data class yet. It’s not final code. I was trying to help people over this learning stage.

      I used the counter in the for loop instead of enumeration only so I could change the limit and initial value easily to try the code against each of the three people.

      If you read this entry take Jeff’s advice for writing your real code!

      Greg

      Comment by gregorykaiser — May 7, 2009 @ 5:35 pm | Reply

  2. Note that person is an instance of Person class with two ivars, one of them: NSDictionary *twitterInfoForUser

    Comment by Jeff — May 7, 2009 @ 1:14 pm | Reply

  3. You should be calling [super viewDidLoad] at the beginning of this routine. most calls to super should be called at the beginning. (except routines like dealloc)

    Comment by dennis — May 21, 2009 @ 1:22 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: