嗨,我有一個類裏面的函數是的NSOperation的一個子類中運行此代碼:無法對象添加到NSMutableArray的
//...
@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數組。有什麼想法?提前致謝!
你可以發佈ParkingAnnotation初始化程序的代碼嗎?有什麼事情會導致它爲零? – 2011-05-10 13:21:45
您已分配self.addedAnnotations。請在For循環之前進行分配。像self.addedAnnotations = [[nsmutablearray alloc] init]; – 2011-05-10 13:21:48
在你的init函數的開頭你有這樣一行:'self.addedAnnotations = nil;'你實際上是否在其他任何地方將這個變量設置爲Mutable Array?或者當你試圖添加它時它是否爲零? – 2011-05-10 13:23:00