如何将带有 NSDictionary 的 NSMutableArray 复制到另一个 NSMutableArray
我有一个 KML 文件,每 10 秒用 NSURL 调用一次。在该方法中,我执行以下操作来存储数据的纬度、经度和颜色。
cachedData 是一个 NSMutableArray 实例变量。
masterData 是一个 NSMutableArray 实例变量。
masterDataCounter 是一个 int 实例变量。
[cachedData addObject:[[[NSDictionary alloc]
initWithObjectsAndKeys:latitude,@"latitude",longitude,@"longitude",color,@"color",nil] autorelease]];
解析数据后,我想将cachedData数组的全部内容保存到关联数组(如上所述的masterData)中,以便我可以通过索引引用它,然后清除cachedData以保存在内存中。
示例
数组索引 |已存储数据
0 | 缓存数据的全部内容
第一次拉取1 | 第二次拉取时缓存数据的全部内容
等。
解析 KML 后,我运行此代码:
[masterData insertObject:cachedData atIndex:masterDataCounter];
[cachedData removeAllObjects];
masterDataCounter++;
这是正确的方法吗?例如,我如何引用第二次运行中存储的数据点总数?
是这样吗?由于某种原因,当我尝试时没有得到正确的结果:
[[masterData objectAtIndex:1] count];
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];
masterDataCounter++;
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于初学者来说,有一种更好的方法来编写我上面描述的段。
对于第二个代码片段,它可以重写为:
从这里是的,您确实可以引用主 masterData NSMutable 数组,如下所示:
For starters there is a better way to write the segment I described above.
For the second code snippet, it could be rewritten as:
From this yes you can indeed reference the main masterData NSMutable array as the following: