次のコードを使用して、オブジェクトを配列に配置しようとしています。その配列に特定の数のオブジェクトが必要です-別の配列の数(36)。
NSArray *sortedKeys = [[self.titlearray objectForKey:@"Title"] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
[self.titlearray setObject:sortedKeys forKey:@"Title"];
NSLog(@"SORTED KEYS COUNT: %i", sortedKeys.count);
for (NSObject *string in sortedKeys) {
NSLog(@"Adding object number %i", [sortedKeys indexOfObject:string]);
NSString *object = [[self.titlearray objectForKey:@"Title"] objectAtIndex:[sortedKeys indexOfObject:string]];
NSArray *latlong = [[self.locationarray objectAtIndex:[[self.titlearray objectForKey:@"Title"] indexOfObject:object]] componentsSeparatedByString:@", "];
CLLocation *firstLocation = [[CLLocation alloc] initWithLatitude:[[latlong objectAtIndex:0] doubleValue] longitude:[[latlong objectAtIndex:1] doubleValue]];
CLLocation *secondLocation = [[CLLocation alloc] initWithLatitude:self.map.userLocation.coordinate.latitude longitude:self.map.userLocation.coordinate.longitude];
CLLocationDistance distance = [secondLocation distanceFromLocation:firstLocation];
float value = distance / 1000;
NSString *distancevalue = [NSString stringWithFormat:@"%.2f", value];
if(self.distances.count < [sortedKeys indexOfObject:object])
[self.distances addObject:distancevalue];
else if([sortedKeys indexOfObject:object] < self.distances.count && [self.distances objectAtIndex:[sortedKeys indexOfObject:object]])
[self.distances replaceObjectAtIndex:[sortedKeys indexOfObject:object] withObject:distancevalue];
}
最初NSLog
は「36」を表示するはずですが、for
ステートメントが呼び出されると、「オブジェクト番号36の追加」のログは表示されません。それは35までしか上がりません。それは非常に奇妙で、なぜこれが起こっているのかわかりません。