我的代碼無法插入一個NSMutableArray的對象:當我解析XML
...
else if ([elementName isEqualToString:@"photo"]) {
NSLog(@"Trying to add photo");
NSLog(@"Prev: %d",[currentItem.photoList count]);
NSLog(@"Author: %@",[currentPhotoItem author ]);
[currentItem.photoList addObject:currentPhotoItem];
NSLog(@"Next: %d",[currentItem.photoList count]);
}
日誌:
2011-10-11 09:49:13.553 ECG [4862:B303 ]嘗試添加照片
2011-10-11 09:49:13.553 ECG [4862:B303]上一個:0
2011-10-11 09:49:13.554 ECG [4862:B303]作者:費爾南多布蘭科
2011-10-11 09:49:13.573 ECG [4862:B303]接下來:0
ADDOBJECT不對象添加到NSMutableArray中的方法,...
「photoList」 被定義
一個的NSMutableString在類:@interface GalleryRSSItem : NSObject {
NSString *title;
NSString *imgtn;
NSString *category;
NSString *url;
NSString *date;
NSMutableArray *photoList;
}
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *imgtn;
@property (nonatomic, retain) NSString *category;
@property (nonatomic, retain) NSString *url;
@property (nonatomic, retain) NSString *date;
@property (nonatomic, retain) NSMutableArray *photoList;
@end
如果你已經正確初始化對象在解析器didStartElemnt方法
- (void) parser:(NSXMLParser *) Parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"gallery"]) {
currentItem = [[GalleryRSSItem alloc] init];
currentNodeContent = [[NSMutableArray alloc] init];
}
if ([elementName isEqualToString:@"photo"]) {
currentPhotoItem = [[PhotoRSSItem alloc] init];
currentNodeContent = [[NSMutableArray alloc] init];
}
}
你確定currentItem.photoList被初始化了嗎? – jbat100
非常感謝,這是錯誤!現在有用! currentItem.photoList = [[NSMutableArray alloc] init]; – Jimmy