I seem to have an odd problem while trying to add objects to a mutable array. The thing is I have an array of dictionaries with coordinates that I get from an external database and I try to do a loop through them, extract the coordinates, create a custom annotation object and then add it to a mutable array.
The problem is the array I get them in shows that it only has 1 object and the first array has 5.
Please help!
Here is the code (remark: testArray is a property on my class i don’t create it bellow, i just try to use it to store the objects)
Thanks!
int times;
int count;
count=[theResults count];
// do the loop oh yeah do the loop
for (times=0;times<count; times=times+1)
{
// create dictionary with contents of array
NSDictionary * testDict = [theResults objectAtIndex:times];
NSLog(@"the results has %i objects", [theResults count]);
NSLog(@"object latitude is %@",[radarDict valueForKey:@"radarLatitude"]);
NSLog(@"object longitude is %@", [radarDict valueForKey:@"radarLongitude"]);
double testLatitude=[[radarDict valueForKey:@"radarLatitude"] doubleValue];
double testLongitude=[[radarDict valueForKey:@"radarLongitude"] doubleValue];
CLLocationCoordinate2D testCoordinate;
testCoordinate.longitude=testLongitude;
testCoordinate.latitude=testLatitude;
CustomAnnotations* tempAnnotation = [[CustomAnnotations alloc] initWithLocation:testCoordinate];
testArray = [[NSMutableArray alloc] initWithCapacity:count];
[testArray addObject:tempAnnotation];
[tempAnnotation release];
}
You are blasting away your array every time you run through the loop. This line is killing you:
Put that before the loop starts and you will be fine.