我正在处理一个让我发疯的问题。我查看了其他论坛,我的代码似乎没问题,但是在为 NSMutableArray 调用 addObject 方法时它失败了。我正在实现一个简单的 xml 解析器类并将一些信息存储在 NSMutableArray 中:
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if([elementName isEqualToString:@"Image"]){
[images setObject: [[imageFile copy] autorelease] forKey: [NSNumber numberWithInt: imageId]];
return;
}
if([elementName isEqualToString:@"Annotation"]){
//Here create the annotation object
Annotation *newAnnot = [[Annotation alloc] init];
newAnnot.imageId = imageId;
newAnnot.annDescription = [[annDescription copy] autorelease];
newAnnot.annLocation = [[annLocation copy] autorelease];
[annotations addObject: newAnnot];
[newAnnot release];
return;
}
if([elementName isEqualToString: @"Patient"] || [elementName isEqualToString: @"Images"] || [elementName isEqualToString: @"Annotations"]){
[currentSection setString: @""];
}else {
[currentElement setString: @""];
}
}
[annotations addObject: newAnnot] 正在使应用程序接收 SIGABRT 信号!
2010-11-08 17:15:00.786 Touches[1430:207] *** -[NSCFDictionary addObject:]: unrecognized selector sent to instance 0x4b1bb50 2010-11-08 17:15:00.788 Touches[1430:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFDictionary addObject:]: unrecognized selector sent to instance 0x4b1bb50' 2010-11-08 17:15:00.789 Touches[1430:207] Stack: (
42174544,
43332396,
42183259,
41645686,
41642482,
18858,
39384333,
70873863,
70897681,
39381417,
17100,
8863,
2234926,
38538931,
38537114,
2234111,
38538931,
38544407,
38545466,
38538931,
38537114,
2230794,
2239193,
103026,
108372,
134462,
115959,
147928,
44216700,
41453724,
41449640,
107041,
140146,
8296 ) terminate called after throwing an instance of 'NSException' Program received signal: “SIGABRT”. (gdb) continue Program received signal: “SIGABRT”.
这里简单描述一下代码:
Annotation 是一个从 NSObject 派生的简单类。我正在实现 init 和 dealloc 方法。第一个什么都不做,只返回“self”,而 dealloc 调用 parent 并释放 2 个字符串
@interface Annotation : NSObject {
int imageId;
NSString *annDescription;
NSString *annLocation;
}
@property (nonatomic, assign) int imageId;
@property (nonatomic, retain) NSString *annDescription;
@property (nonatomic, retain) NSString *annLocation;
直到这里没有什么奇怪的。然后看看实现解析的类:
@interface PatientBundle : NSObject <NSXMLParserDelegate> {
//............
NSMutableArray *annotations;
//.............
}
@property (nonatomic, retain) NSMutableArray *annotations;
我的 init 方法如下所示:
- (id) initFromFile: (NSString*)pathToFile{
//.....
annotations = [[NSMutableArray alloc] initWithCapacity: 10];
[addressParser setDelegate:self];
//......
}
调试表明我添加到“注释”数组中的对象不是零,但是我什至无法插入第一个。为什么我会收到 NSInvalidArgumentException?如果我的数组属于 NSMutableArray 类,为什么要使用 NSCFDictionary :addObject?我缺少关于使用 NSMutableArray 的任何信息吗?
我的平台详细信息: - 运行 iphone Simulator 版本 4.1 (225) - 基础 SDK 和部署目标:iOSDevice 4.1(目标 IPAD) - GCC 4.2
任何线索将不胜感激。提前致谢。
路易斯