无法将对象添加到NSMutableArray

时间:2011-05-10 13:17:44

标签: iphone objective-c cocoa-touch nsmutablearray nsoperation

嘿伙计们,我在一个类中的函数中有这个代码,该类是NSOperation的子类:

//...
@implementation DataLoader

@synthesize addedAnnotations;
@synthesize addedOverlays;
@synthesize loaderFunc;
@synthesize DLDelegate;    
//...
-(id)initWithFunction:(LoaderFunc)func withDelegate:(id)delegate {
    if (self = [super init]) {
        self.addedOverlays = nil;
        self.addedAnnotations = nil;
        self.loaderFunc = func;
        self.DLDelegate = delegate;
        return self;
    }
    return nil;
}
//...
//inside a function
    for (ParkingAnnotations *annotation in fetchedObjects) {
        ParkingAnnotation *parkingAnnot = [[ParkingAnnotation alloc] init];
        workingCoordinate.latitude = [[annotation latitude] doubleValue];
        workingCoordinate.longitude = [[annotation longitude] doubleValue];
        [parkingAnnot setCoordinate:workingCoordinate];
        [parkingAnnot setTitle:[annotation valueForKey:@"lotName"]];
        [parkingAnnot setAnnotationType:[annotation iconTypeRaw]];

        [self.addedAnnotations addObject:parkingAnnot];//parkingAnnot not added to array here
        [parkingAnnot release];
    }
//...

添加了注释是一个NSMutable数组,我一直在使用调试器来处理这段代码,并且由于某种原因,parkingAnnot对象没有被添加到数组中。以下是该类的相关标题代码:

//...    
@interface DataLoader : NSOperation {
        NSMutableArray *addedAnnotations;
    NSMutableArray *addedOverlays;
    LoaderFunc loaderfunc;
    id <DataLoaderProtocol> DLDelegate;
}

@property (nonatomic, retain) NSMutableArray* addedAnnotations;
@property (nonatomic, retain) NSMutableArray* addedOverlays;
@property (nonatomic) LoaderFunc loaderFunc;
@property (assign) id DLDelegate;
//...

这是一个令人惊讶的问题,因为我遇到问题的函数是从我的MapViewController中复制而来的,基本上是相同的,但我代替mapView addAnnotation:添加到NSMutable数组。怎么了解什么了?提前谢谢!

1 个答案:

答案 0 :(得分:4)

您实际在哪里实例化addedAnnotations数组?我只看到它在你的初始化函数中被赋值为nil,也许它应该改为:

self.addedAnnotations = [[[NSMutableArray alloc] init] autorelease];