我正在使用以下代码尝试将对象放入数组中。我需要该数组中特定数量的对象 - 另一个数组的计数(即 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。这很奇怪,我不知道为什么会这样。