I am using the following code to try and put objects in an array. I need a specific number of objects in that array – the count of another array (which is 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];
}
The first NSLog displays ’36’ as it should do, but when the for statement is called, no log appears for “Adding object number 36”. It only goes up to 35. It’s very strange and I am not sure why this is happening.
Array has starting index from 0 to n records.
So it display record count perfectly but when iterated it starts from 0 to n record.
U have 36 record but when iterated it starts from 0 to 35 .