views:

95

answers:

3

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].

+6  A: 

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.

Jack
+1 for thorough ninja post
Shaggy Frog
That was thorough. Thanks very much! I see what you're saying. I may need to rethink what I'm trying to accomplish here.
RyJ
+1  A: 

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.

Shaggy Frog
A: 

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.

David Gelhar