2009-09-08 3 views
1
NSArray *splitPoints = [routeGeom componentsSeparatedByString:@"], ["]; 
routePoints = malloc(sizeof(CLLocationCoordinate2D) * ([splitPoints count] + 1)); 

int i=0; 
NSArray *coords; 
for (NSString* coordStr in splitPoints) { 

    coords = [coordStr componentsSeparatedByString:@","]; 

    routePoints[i].latitude = [[[coords objectAtIndex:0] substringFromIndex:1]floatValue]; 
    routePoints[i].longitude = [[coords objectAtIndex:1] floatValue]; 

    i++; 

} 
[coords release]; 

NSLog(@"** Time to split the route geometry into structs %f", [NSDate timeIntervalSinceReferenceDate] - start); 
+6

나는 얼마나 많은 SO 사용자가이 스레드에서 "StringBuilder! StringBuilder"라고 소리 치고 자신의 모니터에서 C 질문을 보았는지 궁금해. –

답변

6

고려 :

char *buf = [coordStr UTF8String]; 
sscanf(buf, "%f,%f", &routePoints[i].latitude, routePoints[i].longitude); 
2

[coordStr UTF8String]의 반환 된 C- 문자열을 사용하고 수동으로 문자를 구문 분석하는 것이 좋습니다.

2

이 NSScanner는 승리 할 것입니다 경우처럼 나에게 보인다. -componentsSeparatedByString과 -substringFromIndex는 모두 힙 오브젝트를 만들려고합니다. 이것은 꽉짜리 루프에서하고 싶지 않은 것입니다.

2

나는 단지 여기에 뛰어 들어가서 당신의 라인이 [coords release]이 불필요한 (그리고 틀린) 것이라고 말했습니다. GC가 아닌 환경에서 문제가 발생하지 않도록 제거해야합니다. 명시 적으로 만들거나 보관하지 않았기 때문에 coords을 배포 할 필요가 없습니다.

관련 문제