Reputation: 83745
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
Upvotes: 2
Views: 198
Reputation: 35686
Of course it does, because you are not adding the objects to any array...
Obviously you meant [tweets addObject:tweet]
instead of [tweets indexOfObject:tweet]
Upvotes: 2
Reputation: 726987
I think this is because you call indexOfObject:
instead of addObject:
- an innocent autocompletion bug.
Upvotes: 5
Reputation: 894
Try to change last line in for loop to:
[tweets addObject:tweet];
P.S. If tweets
is your array
Upvotes: 2