2012-01-27 2 views
0

배열에 주석을 추가하여지도에 여러 핀을 배치하려고합니다. 나는 for 루프 안에 모든 것을 가지고있다. 처음으로 루프를 반복하면 개체를 배열에 잘 추가합니다. 다시 돌아 가면 ... 배열에 0 개의 객체가 있습니다. 아무도 그 이유를 말할 수 있습니까?NSMutableArray가 조만간 출시 될 것으로 보입니다.

편집 : ARC를 사용하고 있습니다.

-(void)plotMultipleLocs { 
float latitude; 
float longitude; 
NSRange commaIndex; 
NSString *coordGroups; 
for (int i=0; i<=cgIdArray.count; i++) { 
    coordGroups = [cgInAreaArray objectAtIndex:i]; 
    commaIndex = [coordGroups rangeOfString:@","]; 
    latitude = [[coordGroups substringToIndex:commaIndex.location] floatValue]; 
    longitude = [[coordGroups substringFromIndex:commaIndex.location + 1] floatValue]; 
    CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(latitude, longitude); 
    MKCoordinateRegion reg = MKCoordinateRegionMakeWithDistance(loc, 1000, 1000); 
    self->mapView.region = reg; 
    MKPointAnnotation* ann = [[MKPointAnnotation alloc] init]; 
    ann.coordinate = loc; 
    ann.title = [cgNameArray objectAtIndex:i]; 
    ann.subtitle = [cgLocArray objectAtIndex:i]; 
    NSMutableArray *mutAnnArray = [NSMutableArray arrayWithArray:annArray]; 
    [mutAnnArray addObject:ann]; 
} 
} 

답변

4

루프 내에서 변경 가능한 배열을 만들고 그 루프에 개체를 추가하고 있습니다.

루프의 다음 반복에서 새로운 가변 배열을 생성하고 새로운 주석을 추가합니다.

옆으로 다른 배열을 만들기보다는 기본적으로

annArray에 주석을 추가, 당신은 하나의 반복만큼 마지막 객체를 추가하는 배열을 다음 벗어나 있다는 사실 남겨주세요 범위. 루프에서 배열을 이동

시도 : 루프를

-(void)plotMultipleLocs { 
    float latitude; 
    float longitude; 
    NSRange commaIndex; 
    NSString *coordGroups; 

    NSMutableArray *mutAnnArray = [NSMutableArray arrayWithArray:annArray]; // Create one array outside the loop. 

    for (int i=0; i<=cgIdArray.count; i++) { 
     coordGroups = [cgInAreaArray objectAtIndex:i]; 
     commaIndex = [coordGroups rangeOfString:@","]; 
     latitude = [[coordGroups substringToIndex:commaIndex.location] floatValue]; 
     longitude = [[coordGroups substringFromIndex:commaIndex.location + 1] floatValue]; 
     CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(latitude, longitude); 
     MKCoordinateRegion reg = MKCoordinateRegionMakeWithDistance(loc, 1000, 1000); 
     self->mapView.region = reg; 
     MKPointAnnotation* ann = [[MKPointAnnotation alloc] init]; 
     ann.coordinate = loc; 
     ann.title = [cgNameArray objectAtIndex:i]; 
     ann.subtitle = [cgLocArray objectAtIndex:i]; 
     [mutAnnArray addObject:ann]; // Add the annotation to the single array. 
    } 

// mutAnnArray will go out of scope here, so maybe return it, or assign it to a property 
} 
+0

예 ... 동료가 페이지를 새로 고침하기 전에 그것을 알아 냈습니다. 하하. 답변을 주셔서 감사합니다. 나는 당신을 위해 그것을 표시 할 것이다. 그래서 당신은 대표를 얻는다. – tallybear

+0

annArray에서 NSMutableArray로 annArray를 선언하고 작동하지 않는다고 생각하기 때문에 annArray에서 다른 배열을 만듭니다. 배열에 객체가 0 개 있습니다. annArray는 ivar입니다. – tallybear

0

인스턴스가 유지되지 않도록 시도 했습니까?

+0

저는 ARC를 사용하고 있습니다. 내가 그것을 간직하면 그것은 나에게 소리 쳐요. Lol. – tallybear

0

때마다, 당신은 다른 배열의 내용으로 새로운 가변 배열을 만들 수 있습니다. 이전 반복에서 추가 한 객체를 포함하는 변경 가능 배열은 유지되지 않습니다.

관련 문제