How to Copy a NSMutableArray with NSDictionary into another NSMutableArray
I have a KML file I am calling with a NSURL every 10 seconds. In the method I am doing the following to store the latitude, longitude, and color of the data.
cachedData is a NSMutableArray instance variable.
masterData is a NSMutableArray instance variable.
masterDataCounter is an int instance variable.
[cachedData addObject:[[[NSDictionary alloc]
initWithObjectsAndKeys:latitude,@"latitude",longitude,@"longitude",color,@"color",nil] autorelease]];
Once the data is parsed, I want to save the entire contents of the cachedData array into an associative array (masterData as stated above) so I can reference it by indexes and then clear the cachedData to save on memory.
Example
index of array | data stored
0 | the entire contents of cached data on the first pull
1 | the entire contents of cached data on the second pull
etc.
After the KML has parsed, I run this code:
[masterData insertObject:cachedData atIndex:masterDataCounter];
[cachedData removeAllObjects];
ma开发者_C百科sterDataCounter++;
Is this a correct way? How can I reference for example the total number of data points stored in the second run?
Is that right? For some reason I'm not getting correct results when I try it:
[[masterData objectAtIndex:1] count];
For starters there is a better way to write the segment I described above.
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:latitude,@"latitude",longitude,@"longitude",color,@"color",nil];
[cachedData addObject:dict];
[dict release];
For the second code snippet, it could be rewritten as:
[masterData insertObject:[[cachedData copy] autorelease] atIndex:masterDataCounter];
masterDataCounter++;
From this yes you can indeed reference the main masterData NSMutable array as the following:
[[masterData objectAtIndex:desiredIndexNumber] count];
精彩评论