NSArray 中的 NSArray。指针问题?

发布于 2024-08-27 12:22:47 字数 570 浏览 4 评论 0原文

我相信我的问题涉及指针,这是我经常遇到的一个概念,但这就是我想要做的。

我有六个 NSArray。我想要一个由这六个数组组成的额外 NSArray,因此:

self.arr1 = [NSArray array];
self.arr2 = [NSArray array];
self.arr3 = [NSArray array];
self.arr4 = [NSArray array];
self.arr5 = [NSArray array];
self.arr6 = [NSArray array];

NSArray *containerArray = [[NSArray alloc] initWithObjects:self.arr1, ... etc, nil];

每当我更新前 6 个 NSArray 之一时,我都希望在 containerArray 中更新该对象。 (我知道我使用的是 NSArray 而不是 NSMutableArray,当我更新数组时,我创建一个新数组并将其分配给实例变量)。

目前,对 arr1 的任何操作都不会反映在 [containerArray objectAtIndex:0] 中。

I believe my problem involves pointers, a concept I often struggle with, but here's what I'm trying to do.

I have six NSArrays. I want an additional NSArray comprised of these six arrays, so:

self.arr1 = [NSArray array];
self.arr2 = [NSArray array];
self.arr3 = [NSArray array];
self.arr4 = [NSArray array];
self.arr5 = [NSArray array];
self.arr6 = [NSArray array];

NSArray *containerArray = [[NSArray alloc] initWithObjects:self.arr1, ... etc, nil];

Whenever I update one of the first 6 NSArrays, I want the object updated in containerArray. (I know I'm using an NSArray and not an NSMutableArray, when I update the arrays I create a new one and assign it to the instance variable).

Currently, any manipulation of arr1 is not reflected in [containerArray objectAtIndex:0].

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

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

发布评论

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

评论(3

记忆之渊 2024-09-03 12:22:48

因为您的实例变量 self.arr1self.arr2NSArray 类型变量的占位符,但是当您更新它们时,您只会丢失仍保留在 containerArray 中的旧引用。

为了更好地解释:

self.arr1 <------ [NSArray array] (the first one you create)

然后您构建了 contentArray 并且您拥有:

self.arr1 <--------------------------+-- [NSArray array] (first one you create)
                                     |
[contentArray objectAtIndex:0] <-----+   

在创建 contentArray 并将数组分配给它之后,您就有了重复的引用。 self.arr1 将指向 contentArray 的第一个元素所指向的同一数组,依此类推。

然后你用一个新数组更新 self.arr1 = [NSArray array] 所以你得到的是

self.arr1 <------------ [NSArray array] (new one created)

[contentArray objectAtIndex:0] <----- [NSArray array] (the old one)

那么发生了什么?

您创建了一个新数组并将其分配给 self.arr1。因此,self.arr1 将指向新项目,而 [contentArray objectAtIndex:0] 仍将引用旧项目。对 self.arr1 的每一项修改都不会反映在另一项修改上,因为它们不是同一个对象。

这是因为您使用的是对象的引用,而不是普通对象。当您将一个新数组分配给 self.arr1 时,您不会修改旧数组,而是会放弃对它的引用,以用新数组替换旧数组。

这就是为什么您应该使用 NSMutableArray :因为在这种情况下您将删除/添加元素到同一数组,而不创建新的数组。

Because your instance variables self.arr1, self.arr2 are placeholders for a variable of type NSArray, but when you update them you just lose the old reference that still remains in the containerArray.

To explain better:

self.arr1 <------ [NSArray array] (the first one you create)

then you build up the contentArray and you have:

self.arr1 <--------------------------+-- [NSArray array] (first one you create)
                                     |
[contentArray objectAtIndex:0] <-----+   

after you create your contentArray and you assign the arrays to it you have duplicated references. self.arr1 will point to the same array pointed by first element of contentArray and so on for others.

Then you update self.arr1 = [NSArray array] with a new array so what you obtain is

self.arr1 <------------ [NSArray array] (new one created)

[contentArray objectAtIndex:0] <----- [NSArray array] (the old one)

So what's happened?

You created a new array and assigned it to self.arr1. So self.arr1 will point to the new item while [contentArray objectAtIndex:0] will still reference to the old one. Every modification to self.arr1 will be not reflected on the other one because they are NOT the same object.

This because you are using references to objects and not plain objects. When you assign a new array to self.arr1 you are not modifying the old one but discarding the reference to it for a new one that replaces the old one.

That's why you should use a NSMutableArray: because in that case you would remove/add elements to the same array, without creating a new one.

夜唯美灬不弃 2024-09-03 12:22:48

实际上,NSArray 将为您提供您想要的行为,因为它维护对您提供的对象的强引用。来自 NSArray 类参考

数组维护对其内容的强引用 - 在托管内存环境中,每个对象在将其 id 添加到数组之前都会收到保留消息,并且在从数组中删除或释放数组时会收到释放消息。< /p>

与此同时,杰克似乎已经发布了我即将给出的解释,所以我就到此为止。

Actually, NSArray will provide you with the behaviour you want, since it maintains strong references to the objects you give it. From the NSArray Class Reference:

Arrays maintain strong references to their contents—in a managed memory environment, each object receives a retain message before its id is added to the array and a release message when it is removed from the array or when the array is deallocated.

And in the meantime it looks like Jack posted the explanation I was about to give, so I'll leave it at that.

一曲爱恨情仇 2024-09-03 12:22:48

如果您对各个数组使用可变数组,而不是创建新数组并重新分配它们,那么事情的表现会更像您所期望的那样。

If you used mutable arrays for the individual arrays instead of creating new ones and re-assigning them, things would behave more like you're expecting.

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