2011-05-10 90 views
2

嗨,我有一個類裏面的函數是的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數組。有什麼想法?提前致謝!

+0

你可以發佈ParkingAnnotation初始化程序的代碼嗎?有什麼事情會導致它爲零? – 2011-05-10 13:21:45

+1

您已分配self.addedAnnotations。請在For循環之前進行分配。像self.addedAnnotations = [[nsmutablearray alloc] init]; – 2011-05-10 13:21:48

+0

在你的init函數的開頭你有這樣一行:'self.addedAnnotations = nil;'你實際上是否在其他任何地方將這個變量設置爲Mutable Array?或者當你試圖添加它時它是否爲零? – 2011-05-10 13:23:00

回答

4

你究竟在哪裏實例化addedAnnotations數組?我只看到它在初始化函數中被賦值爲零,也許它應該變成類似於:

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

該死的,我知道這件事很簡單,謝謝你幫我看看。這就是訣竅! – Stunner 2011-05-10 13:39:44

+1

@PeyloW爲什麼32?爲了內存對齊的目的? – iceydee 2011-05-10 14:32:20

+1

這次沒有特別的原因。有些號碼是必需的,32個感覺很好。在現實世界中,您需要一個足夠大的數字,以便在正常使用情況下不需要增加可變陣列容量。由於增加陣列的尺寸是昂貴的。 – PeyloW 2011-05-10 14:49:07

相關問題