在 XCode 中创建数组的数组

发布于 2024-08-12 01:19:11 字数 945 浏览 6 评论 0原文

我正在尝试在 Xcode 中构建一个包含字典对象数组的数组。我可以创建一个包含一个或多个字典的工作数组,然后使用 addObject: 方法将此数组作为对象添加到我的主数组中。我循环了几次,重建工作数组并向主数组添加对象。到目前为止一切都很好,除了在检查我的主数组时它包含构建工作数组所用的最后一个字典的重复项。我假设这是因为当我使用 addObject 时,它只是分配一个指向数组的指针,但不会增加保留计数或创建工作数组的副本,因此每次重建时,主数组只是指向此数组。

我想我的问题是如何创建工作数组的副本将其添加到主数组中,然后重建它并再次添加它?希望这是有道理的,抱歉,如果这似乎是一个基本问题 - 确实如此(我是 Xcode 新手) - 任何帮助将不胜感激。


        [sectionArray release];
        sectionArray  = [[NSMutableArray alloc] init];
        [sectionArray addObject:dict];

这一切都有效,这太棒了。我还没有弄清楚的一件事是,当我完成主数组并执行 [mainArray 释放] 时,是否存在危险,这些子数组将留在内存中,或者释放会破坏他们也是吗?


我已经通过重新初始化内部工作数组而不是仅仅删除所有对象来解决这个问题,即,

我确实有:

        [sectionArray removeAllObjects];
        [sectionArray addObject:dict];

并将其更改为:

        sectionArray  = [[NSMutableArray alloc] init];
        [sectionArray addObject:dict];

我有一种感觉,但这不是一件好事,我会开始泄漏到处都是记忆。

I am trying to build an array that contains arrays of dictionary objects in Xcode. I can create a working array that has one or more dictionaries in it and then use the addObject: method to add this array as an object in my main array. I cycle around this several times, rebuilding the working array and adding objects to the main array. All good so far, except that on inspecting my main array it contains duplicates of the last dictionaries that the working array was built with. I am assuming this is because when I use the addObject it simply assigns a pointer to the array but does not increase the retain count or create a copy of the working array, hence every time it is rebuilt the main array simply points to the this array.

I guess my question is how do I create a copy of the working array add it to the main array and then rebuild it and add it again? Hope that makes sense and sorry if this appears to be a basic question - it is (I'm new to Xcode) - and any help would be really appreciated.


        [sectionArray release];
        sectionArray  = [[NSMutableArray alloc] init];
        [sectionArray addObject:dict];

This all works, which is great. One thing I don't have my head around yet is, is there a danger that when I am done with the main array and I do a [mainArray release] too that these child arrays will be left behind in memory or will the release destroy them as well?


I have solved it by re-initialising the inner working array instead of just removing all objects, i.e.,

I did have:

        [sectionArray removeAllObjects];
        [sectionArray addObject:dict];

And changed it to:

        sectionArray  = [[NSMutableArray alloc] init];
        [sectionArray addObject:dict];

I have a feeling though that this is not a good thing to do and I will start to leak memory all over the place.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

〗斷ホ乔殘χμё〖 2024-08-19 01:19:11

如果您对 sectionArray 所做的只是将其放入另一个数组中,则需要在将其放入 mainArray 后释放它,以便只有 mainArray保存对它的引用:

sectionArray = [[NSMutableArray alloc] init];
[sectionArray addObject:dict];
[mainArray addObject:sectionArray];
[sectionArray release];

当您稍后释放 mainArray 引用时,它将负责释放它的所有子级,并且您的 sectionArray 将被释放。 (对于放在 sectionArray 中的字典也是如此)。

希望这有帮助。

If all you do with sectionArray is to put it in another array you need to release it after you put it in your mainArray so that only mainArray holds a reference to it:

sectionArray = [[NSMutableArray alloc] init];
[sectionArray addObject:dict];
[mainArray addObject:sectionArray];
[sectionArray release];

When you later release your mainArray reference it will take care of releasing all it's children and your sectionArray's will be freed. (The same goes for the dicts put in sectionArray).

Hope this helped.

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