views:

233

answers:

4
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);
+2  A: 

I would consider using [coordStr UTF8String]'s returned c-string and manually parsing the characters.

Ben Gottlieb
+6  A: 

Consider:

char *buf = [coordStr UTF8String];
sscanf(buf, "%f,%f", &routePoints[i].latitude, routePoints[i].longitude);
Will Hartung
+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
+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