现在我一次遍历一个数组并计算距离,如下所示:
- (void)calculateDistance
{
ann = [dict objectForKey:@"Blue"];
for(int i = 0; i < [ann count]; i++) {
NSString *coordinates = [[ann objectAtIndex:i] objectForKey:@"Coordinates"];
double realLatitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:1] doubleValue];
double realLongitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:0] doubleValue];
// Calculating distance
CLLocation *pinLocation = [[CLLocation alloc]
initWithLatitude:realLatitude
longitude:realLongitude];
CLLocation *userLocation = [[CLLocation alloc]
initWithLatitude:mapView.userLocation.coordinate.latitude
longitude:mapView.userLocation.coordinate.longitude];
CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];
// Adding distance to dictionaries
if (distance > 1000) {
NSString *dist = [NSString stringWithFormat:@"%.2f km.", distance/1000];
NSMutableDictionary *inDict = [[NSMutableDictionary alloc] init];
inDict = [ann objectAtIndex:i];
[inDict setValue:dist forKey:@"Distance"];
}
else{
NSString *dist = [NSString stringWithFormat:@"%4.0f m.", distance];
NSMutableDictionary *inDict = [[NSMutableDictionary alloc] init];
inDict = [ann objectAtIndex:i];
[inDict setValue:dist forKey:@"Distance"];
}
}
}
我的数据结构是:
如何一次循环遍历所有数组?我有一个数组,其中包含我所有的名为“ resultArray ”的数组,但是这段代码不起作用:
ann = [dict objectForKey:resultArray];
NSLog(@"%@", resultArray);
2013-05-05 10:57:03.643 testApp[5708:907] (
Black,
Green,
Orange,
Blue,
Darkblue
)