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);
views:
233answers:
4
+2
A:
I would consider using [coordStr UTF8String]'s returned c-string and manually parsing the characters.
Ben Gottlieb
2009-09-09 00:14:12
+6
A:
Consider:
char *buf = [coordStr UTF8String];
sscanf(buf, "%f,%f", &routePoints[i].latitude, routePoints[i].longitude);
Will Hartung
2009-09-09 00:18:57
+2
A:
This looks to me like a case where NSScanner would be a win. -componentsSeparatedByString and -substringFromIndex are both going to create heap objects, which is something you don't want to be doing in a tight loop.
NSResponder
2009-09-09 02:05:21
+2
A:
I just thought I'd jump in here and say that your line [coords release]
is unnecessary (and wrong). You should remove it to avoid problems in non-GC environments. You do not have to release coords
because you did not explicitly create or retain it.
dreamlax
2009-09-09 02:40:56