2010-11-08 75 views
0

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

任何线索将被亲切地赞赏。提前致谢。

路易斯

+5

无论发生什么事,都不会从您发布的代码中看到。不知何故'annotations'是一个NSDictionary而不是一个数组;很可能指针正在被覆盖。 – 2010-11-08 17:01:28

+0

您的注释属性是可读/写的,而不是只读的,因为我期望它是在类的init中创建的mutableArray。为什么?也许你不小心在某个地方打电话给二传手? – 2010-11-08 17:31:07

回答

2

Unrecognized selector sent to <blah>几乎总是意味着所讨论的对象被释放,和(不同类型的)另一个目的使用相同的存储以后被分配的。

要追踪这个,请查看setting NSZombieEnabled,它将所有释放的对象保留为“僵尸”,而不是实际释放内存。

0

约翰的评论值得研究。针对所有出现的“注释”进行项目范围查找。另外,使用调试器来确保你认为它停止的行真的是正确的行。

另外:当我将属性P称为普通P而不是self.P时,我在应用程序中出现了奇怪的行为。我认为你应该通过你的代码并做出这个改变。

刚刚看到David的评论,我认为这与我的评论有关。普通P不保留; self.P确实。解决这个问题,你的麻烦可能会结束。

2

错误显示您要发送的对象addObject:to是NSDictionary。 (记住一个对象的实际类别可能与您声明它的方式不同,即使这些对象正在工作。)如果您有一个陈旧的指针,可能会发生这种情况 - 尝试使用NSZombieEnabled来跟踪该指针。

作为回调,您的PatientBundle甚至有可能不再有效。