2012-04-02 79 views
2

基本上,我有一個包含時間軸的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 

回答

5

我認爲這是因爲你打電話indexOfObject:而不是addObject: - 一個無辜的自動補全錯誤。

2

嘗試將最後一行的for循環更改爲:

[tweets addObject:tweet]; 

PS如果tweets是你的陣列

2

當然有,是因你沒有添加對象任何數組... 顯然你的意思[tweets addObject:tweet]而不是[tweets indexOfObject:tweet]