2011-05-10 12 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 initializer에 대한 코드를 게시 할 수 있습니까? 아무 일도 일어나지 않을 겁니다. –

+1

self.addedAnnotations를 할당했습니다. For 루프 전에 할당하십시오. self.addedAnnotations = [[nsmutablearray alloc] init]; –

+0

init 함수의 시작 부분에 다음 줄이 있습니다 :'self.addedAnnotations = nil;'실제로이 변수를 다른 곳의 가변 배열로 설정 하시겠습니까? 아니면 그것에 추가하려고 할 때 그것은 아무것도 아닌가요? –

답변

4

실제로 addedAnnotations 배열을 인스턴스화하고 있습니까? 나는 초기화 기능에서 nil이 할당 된 것을 본다. 아마도 다음과 같이 바뀌어야한다.

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

젠장, 나는 그게 뭔가 간단하다는 것을 알았어, 내가 그것을 볼 수있게 도와 줘서 고마워. 그 트릭을 했어! – Stunner

+1

@PeyloW 왜 32입니까? 메모리 정렬을 위해서? – iceydee

+1

이번에는 특별한 이유가 없습니다. 어떤 번호는 필수이고 32는 좋았습니다. 현실 세계에서는 정상적인 사용 사례에서 증가 할 배열 용량을 변경할 필요가 없도록 충분히 큰 숫자를 원합니다. 어레이의 크기를 증가시키는 것은 많은 비용이 듭니다. – PeyloW

관련 문제