Cocoa: Error while adding objects from a loop to a mutable array
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];
}
Your problem is that you're not adding these items to your array, instead you are creating a new array every time and over-writing the old one. Then you add an item to that new array and continue. Therefore you will have count - 1 leaked arrays and the final array, each with one item.
Before you go into your loop, do something like:
[testArray autorelease];
testArray = [[NSMutableArray alloc] initWithCapacity:count];
// start the loop
for( /* ... */ ) {
// stuff
[testArray addObject:tempAnnotation];
// etc...
}
You are blasting away your array every time you run through the loop. This line is killing you:
testArray = [[NSMutableArray alloc] initWithCapacity:count];
Put that before the loop starts and you will be fine.
Sosborn's answer is correct and all you need to do here is ensure your array is initialized once and each iteration does not overwrite it.
I do want to add one thing regarding the iteration of array which I think will be favorable for you in the future. Consider the fact that arrays have enumerators and that the for loop syntax can be very much simplified using the enumerator technique vs. the traditional for syntax.
I've simplified your code in the way I am discussing:
for (NSDictionary *testDict in theResults)
{
//Do what you need to with an instance of a dictionary from the array
}
This just makes it easier because you do not have to first find out the number of items in the array. It knows automatically how many it should iterate through. Additionally you are not responsible for getting the conditional statement of the loop correct or dealing with incrementing an int to keep track. If you think this was helpful please vote up.
精彩评论