views:

445

answers:

1

Hello,

I have an NSArray called 'objects' below with arrayCount = 1000. It takes about 10 secs to iterate through this array. Does anyone have a faster method of iterating through this array?

Thanks!

for (int i = 0; i <= arrayCount; i++) {

event.latitude = [[[objects valueForKey:@"CLatitude"] objectAtIndex:i] floatValue];
event.longitude = [[[objects valueForKey:@"CLongitude"] objectAtIndex:i] floatValue];

}

+1  A: 

It looks like the arrays returned by valueForKey will be the same at every iteration so try obtaining a reference to them once outside the loop:

NSArray *latitudes = [objects valueForKey:@"CLatitude"];
NSArray *longitudes = [objects valueForKey:@"CLongitude"];
for (int i = 0; i <= arrayCount; i++) {
    event.latitude = [[latitudes objectAtIndex:i] floatValue];
    event.longitude = [[longitudes objectAtIndex:i] floatValue];
}

But what is the point of the loop? In the end, doesn't it just set the event properties to the last values in the two arrays? Also, if arrayCount is the number of elements then the loop should be up to i < arrayCount instead of i <= arrayCount.

DyingCactus