0

我正在处理一个让我发疯的问题。我查看了其他论坛,我的代码似乎没问题,但是在为 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

任何线索将不胜感激。提前致谢。

路易斯

4

3 回答 3

2

The error shows that the object you're sending addObject: to is an NSDictionary. (Remember that the actual class of an object can differ from how you've declared it, even if things are working.) This could happen if you have a stale pointer -- try NSZombieEnabled to track that one.

As a callback, it's even possible that your PatientBundle is no longer valid.

于 2010-11-08T17:25:44.763 回答
2

Unrecognized selector sent to <blah>几乎总是意味着有问题的对象被释放,另一个对象(不同类型的)后来使用相同的存储分配。

要追踪这一点,请查看设置 NSZombieEnabled,它将所有已释放的对象保持为“僵尸”,而不是实际释放内存。

于 2010-11-08T17:23:31.113 回答
0

John's comment is worth looking into. Do a project-wide find for all occurrences of "annotations". Also, use the debugger to ensure that the line you think it's stopping on is really the right one.

Also: I have had weird behavior in my apps when I refer to a property P as plain P instead of self.P. I think you should go through your code and make this change.

Just saw David's comment, which I think is connected with my comment. Plain P does not do the retain; self.P does. Fix this and your troubles may be over.

于 2010-11-08T17:24:05.220 回答