开发者

How to Copy a NSMutableArray with NSDictionary into another NSMutableArray

开发者 https://www.devze.com 2023-03-20 14:07 出处:网络
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.

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消