基本上,我有一個包含時間軸的Twitter API的JSON響應。我想在一個循環與分享Tweet對象來填充和數組,但警告窗口,告訴我,在循環之後數組爲空:這個for循環有什麼問題 - 試圖用Tweet對象填充數組?
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];
鳴叫目標很簡單:
@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