这个循环非常慢,我想因为我创建了很多中间字符串.我怎样才能加快速度?

And*_*son 1 iphone cocoa-touch objective-c

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);
Run Code Online (Sandbox Code Playgroud)

Wil*_*ung 6

考虑:

char *buf = [coordStr UTF8String];
sscanf(buf, "%f,%f", &routePoints[i].latitude, routePoints[i].longitude);
Run Code Online (Sandbox Code Playgroud)