Basically, I have a JSON response from the Twitter API containing a timeline. I am trying to fill and array with Tweet objects in a loop but the alert window tells me that after the loop the array is empty:
NSError *error;
NSArray *tweetJsonObjects = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
for (int i = 0; i < [tweetJsonObjects count]; i++) {
Tweet *tweet = [[Tweet alloc] init];
tweet.userName = [[[tweetJsonObjects objectAtIndex:i] objectForKey:@"user"] objectForKey:@"name"];
tweet.text = [[tweetJsonObjects objectAtIndex:i] objectForKey:@"text"];
//[tweet.text gtm_stringByUnescapingFromHTML];
tweet.userProfileImageUrl = [[[tweetJsonObjects objectAtIndex:i] objectForKey:@"user"] objectForKey:@"profile_image_url"];
[tweets addObject:tweet];
}
NSString *x = [NSString stringWithFormat:@"%d", [tweets count]];
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!"
message:x
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
The Tweet object is very simple:
@interface Tweet : NSObject
{
NSString *userName;
NSString *text;
NSString *userProfileImageUrl;
UIImage *userProfileImage;
}
@property (nonatomic, retain) NSString *userName;
@property (nonatomic, retain) NSString *text;
@property (nonatomic, retain) NSString *userProfileImageUrl;
@property (nonatomic, retain) UIImage *userProfileImage;
@end
I think this is because you call
indexOfObject:instead ofaddObject:– an innocent autocompletion bug.