2011-05-15 136 views
1

我無法將對象添加到簡單的NSMutableArray。我在我旁邊有一個Objective-c引用,並且它爲簡單的字符串工作。但我需要添加實際的對象。請告訴我我在這裏做錯了什麼。爲什麼我的對象不被添加到數組中?

代碼:

TBXMLElement *hubImage = [TBXML childElementNamed:@"image" parentElement:[TBXML childElementNamed:@"images" parentElement:pieceXML]]; 
if(hubImage) { 
    do { 
     HubPieceImage *tmpImage = [[HubPieceImage alloc] initWithXML:hubImage]; 
     [self.images addObject: tmpImage]; 
     HubPieceImage *tmpImage2 = [self.images lastObject]; 
     NSLog(@"image : %@", tmpImage.asset_url); 
     NSLog(@"image 2: %@", tmpImage2.asset_url); 
    } while ((hubImage = hubImage->nextSibling)); 
} 
NSLog(@"count : %i", [self.images count]); 

返回此日誌,因爲它遍歷兩個對象:

image : http://farm6.static.flickr.com/5215/5533190578_4v629a79e5.jpg 
image 2: (null) 
image : http://farm3.static.flickr.com/2774/5416668522_fdcr19aed3.jpg 
image 2: (null) 
count : 0 

其實陣列似乎根本不填滿(考慮數:0)

感謝您的幫助

+1

是什麼self.images實施什麼樣子的? – servn 2011-05-15 07:04:38

回答

0

我發現問題了!
我沒有初始化數組,像這樣:

self.images = [[NSMutableArray alloc] init]; 
+0

如果它是一個保留屬性,您可能需要添加一個'-autorelease'消息。 – 2011-05-15 07:36:33

+0

不要忘記接受你的回答,以免人們認爲這個問題還沒有解決。 – idz 2011-05-15 07:36:50

2

只是爲了好玩試試這個:

HubPieceImage *tmpImage = [[HubPieceImage alloc] initWithXML:hubImage]; 
    NSAssert(self.images, @"Whoops! self.images is NULL"); 
    [self.images addObject: tmpImage]; 

只可能是您的問題!也就是說,self.images可能是零。

0
images = [[NSMutableArray alloc] initWithCapacity:10]; 

請確保您有人稱alloc和初始化之前

相關問題