現在我遍歷一個數組循環一次和計算距離是這樣的:如何通過陣列
- (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"];
}
}
}
我的數據結構爲:
如何通過所有陣列的在循環一旦?我有數組,它包含我所有的Array的命名爲 「resultArray」,但是這個代碼不工作:
ann = [dict objectForKey:resultArray];
的NSLog(@ 「%@」,resultArray);
2013-05-05 10:57:03.643 testApp[5708:907] (
Black,
Green,
Orange,
Blue,
Darkblue
)
「不起作用」是非信息性的。告訴我們究竟是什麼錯誤。 – 2013-05-05 08:07:41
如果我這樣做:ann = [dict objectForKey:resultArray];距離不添加到字典,如果我這樣做:ann = [dict objectForKey:@「Blue」];或ann = [dict objectForKey:@「Yellow」];等等。距離增加到藍色,黃色字典 – 2013-05-05 08:17:18
你可以檢查我的答案。 – Anupdas 2013-05-05 09:20:26