如何将带有 NSDictionary 的 NSMutableArray 复制到另一个 NSMutableArray

发布于 2024-11-24 01:31:55 字数 892 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风流物 2024-12-01 01:31:55

对于初学者来说,有一种更好的方法来编写我上面描述的段。

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:latitude,@"latitude",longitude,@"longitude",color,@"color",nil];
[cachedData addObject:dict];
[dict release];

对于第二个代码片段,它可以重写为:

[masterData insertObject:[[cachedData copy] autorelease] atIndex:masterDataCounter];
masterDataCounter++;

从这里是的,您确实可以引用主 masterData NSMutable 数组,如下所示:

[[masterData objectAtIndex:desiredIndexNumber] 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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文