NSArray 中的 NSArray。指针问题?
我相信我的问题涉及指针,这是我经常遇到的一个概念,但这就是我想要做的。
我有六个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为您的实例变量
self.arr1
、self.arr2
是NSArray
类型变量的占位符,但是当您更新它们时,您只会丢失仍保留在containerArray
中的旧引用。为了更好地解释:
然后您构建了
contentArray
并且您拥有:在创建
contentArray
并将数组分配给它之后,您就有了重复的引用。self.arr1
将指向contentArray
的第一个元素所指向的同一数组,依此类推。然后你用一个新数组更新 self.arr1 = [NSArray array] 所以你得到的是
那么发生了什么?
您创建了一个新数组并将其分配给
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 typeNSArray
, but when you update them you just lose the old reference that still remains in thecontainerArray
.To explain better:
then you build up the
contentArray
and you have: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 ofcontentArray
and so on for others.Then you update
self.arr1 = [NSArray array]
with a new array so what you obtain isSo what's happened?
You created a new array and assigned it to
self.arr1
. Soself.arr1
will point to the new item while[contentArray objectAtIndex:0]
will still reference to the old one. Every modification toself.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.实际上,
NSArray
将为您提供您想要的行为,因为它维护对您提供的对象的强引用。来自 NSArray 类参考:与此同时,杰克似乎已经发布了我即将给出的解释,所以我就到此为止。
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:And in the meantime it looks like Jack posted the explanation I was about to give, so I'll leave it at that.
如果您对各个数组使用可变数组,而不是创建新数组并重新分配它们,那么事情的表现会更像您所期望的那样。
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.