NSMutableArray的addObject崩溃

NSMutableArray的addObject崩溃

本文介绍了NSMutableArray的addObject崩溃!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个使我生气的问题.我看过其他论坛,我的代码似乎还可以,但是为NSMutableArray调用addObject方法时,它失败了.我正在实现一个简单的xml解析器类,并将一些信息存储在NSMutableArray中:

I am dealing with a problem which is driving me mad. I have looked at other forums and my code seems to be ok, however it fails when invoking addObject method for NSMutableArray. I am implementing a simple xml parser class and storing some of the information in a 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信号!

[annotations addObject: newAnnot] is making the application to receive a SIGABRT signal!

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".

下面是代码的简要说明:

Here a brief description of the code:

Annotation是一个从NSObject派生的简单类.我正在实现init和dealloc方法.第一个不执行任何操作,仅返回"self",而dealloc调用parent并释放2个字符串

Annotation is a simple class derived from NSObject. I am implementing init and dealloc methods. The first one does nothing, just returns "self" whereas dealloc invokes parent and releases the 2 strings

@interface Annotation : NSObject {
    int imageId;
    NSString *annDescription;
    NSString *annLocation;
}

@property (nonatomic, assign) int imageId;
@property (nonatomic, retain) NSString *annDescription;
@property (nonatomic, retain) NSString *annLocation;

直到这里都没什么奇怪的.然后看一下实现解析的类:

Nothing strange until here. Then have a look at the class implementing the parsing:

@interface PatientBundle : NSObject <NSXMLParserDelegate> {
    //............
    NSMutableArray *annotations;
    //.............
}
@property (nonatomic, retain) NSMutableArray *annotations;

我的初始化方法如下:

- (id) initFromFile: (NSString*)pathToFile{
    //.....
    annotations = [[NSMutableArray alloc] initWithCapacity: 10];
    [addressParser setDelegate:self];
    //......
}

调试表明我要添加到注释"数组中的对象不是nil,但是我什至无法插入第一个对象.为什么会收到NSInvalidArgumentException?如果我的数组属于NSMutableArray类,为什么要使用NSCFDictionary:addObject?我缺少NSMutableArray的使用吗?

Debugging suggests the object I am adding into the "annotations" array is not nil, however I am unable to even insert the first one. Why do I get a NSInvalidArgumentException? Why NSCFDictionary :addObject if my array is of class NSMutableArray? Is anything about the use of NSMutableArray that I am missing?

我的平台详细信息:-运行iphone模拟器4.1版(225)-基础SDK和部署目标:iOSDevice 4.1(目标IPAD)-GCC 4.2

My platform details:- Running iphone Simulator Version 4.1 (225)- Base SDK and deployment target: iOSDevice 4.1 (target IPAD)- GCC 4.2

任何线索将不胜感激.预先感谢.

Any clue will be kindly appreciated. Thanks in advance.

路易斯

推荐答案

该错误表明您要向其发送addObject:的对象是NSDictionary.(请记住,即使工作正常,对象的实际类也可能与您声明的类不同.)如果您的指针过时,则可能会发生这种情况,请尝试使用NSZombieEnabled来跟踪该对象.

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.

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

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

这篇关于NSMutableArray的addObject崩溃!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:01