为什么我的对象不被添加到数组中?
问题描述:
我无法将对象添加到简单的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)
感谢您的帮助
答
我发现问题了!
我没有初始化数组,像这样:
self.images = [[NSMutableArray alloc] init];
+0
如果它是一个保留属性,您可能需要添加一个'-autorelease'消息。 – 2011-05-15 07:36:33
+0
不要忘记接受你的回答,以免人们认为这个问题还没有解决。 – idz 2011-05-15 07:36:50
答
只是为了好玩试试这个:
HubPieceImage *tmpImage = [[HubPieceImage alloc] initWithXML:hubImage];
NSAssert(self.images, @"Whoops! self.images is NULL");
[self.images addObject: tmpImage];
只可能是您的问题!也就是说,self.images可能是零。
答
images = [[NSMutableArray alloc] initWithCapacity:10];
请确保您有人称alloc和初始化之前
是什么self.images实施什么样子的? – servn 2011-05-15 07:04:38