我正在处理一个让我发疯的问题。我看过其他论坛,我的代码似乎没问题,但是它调用addObject方法为NSMutableArray失败。我采取一个简单的XML解析器类和存储的一些信息在一个的NSMutableArray:addObject for 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: @""];
}
}
[注解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”.
这里代码的简要说明:
注释是从NSObject的衍生的简单类。我正在实现init和dealloc方法。第一个什么也不做,只是返回的“自我”,而dealloc的调用父和释放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?为什么NSCFDictionary:addObject如果我的数组是类NSMutableArray?有什么关于使用NSMutableArray我缺少的?
我的平台详细信息: - 运行iPhone模拟器版本4.1(225) - 基本SDK和部署目标:iOSDevice 4.1(目标IPAD) - GCC 4.2
任何线索将被亲切地赞赏。提前致谢。
路易斯
无论发生什么事,都不会从您发布的代码中看到。不知何故'annotations'是一个NSDictionary而不是一个数组;很可能指针正在被覆盖。 – 2010-11-08 17:01:28
您的注释属性是可读/写的,而不是只读的,因为我期望它是在类的init中创建的mutableArray。为什么?也许你不小心在某个地方打电话给二传手? – 2010-11-08 17:31:07