NSArray/NSMutableArray : Passed by ref or by value???

Posted by wgpubs on Stack Overflow See other posts from Stack Overflow or by wgpubs
Published on 2010-04-16T01:13:56Z Indexed on 2010/04/16 1:23 UTC
Read the original article Hit count: 299

Totally confused here.

I have a PARENT UIViewController that needs to pass an NSMutableArray to a CHILD UIViewController. I'm expecting it to be passed by reference so that changes made in the CHILD will be reflected in the PARENT and vice-versa. But that is not the case. Both have a property declared as ..

@property (nonatomic, retain) NSMutableArray *photos;

Example:

In PARENT:

self.photos = [[NSMutableArray alloc] init];

ChildViewController *c = [[ChildViewController alloc] init ...];
c.photos = self.photos;
...
...

...

In CHILD:

[self.photos addObject:obj1];
[self.photos addObject:obj2];

NSLog(@"Count:%d", [self.photos count]) // Equals 2 as expected

...

Back in PARENT:

NSLog(@"Count:%d", [self.photos count])  // Equals 0 ... NOT EXPECTED

I thought they'd both be accessing the same memory. Is this not the case? If it isn't ... how do I keep the two NSMutableArrays in sync?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about nsarray