views:

201

answers:

2

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?

UPDATE with some more code ...

IN PARENT UIViewController:

     - (void)loadView { 

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

         // Create view for root controller
     UIView *rootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
     self.view = rootView;
     self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"green_felt_bg.jpg"]];
     [rootView release];

     ChildViewController *c = [[ChildViewController alloc] initWithNibName:@"ChildView" bundle:nil];
     self.childViewController = c;
     [c release];

     self.childViewController.photos = self.photos;
     self.childViewController.delegate = self;

     [self.view insertSubview:self.childViewController.view atIndex:0];
 } 

In the CHILD ViewController I'm just adding objects into it. Calling a delegate method when it is done adding objects ... which the PARENT handles. Nothing else significant going on. Code child is using to modify its "photos" property ..

[[self mutableArrayValueForKey:@"photos"] 
        addObject:[photo resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(200.0f, 300.0f) interpolationQuality:kCGInterpolationLow]];
A: 

The easiest way to do this would be to not use the synthesized accessor methods, and just roll your own get/set methods that specifically set the pointer references equal.

Looking at the apple documentation on properties, you can still use the convenient dot notation with properties even if you don't use the @synthesize directive.

My guess is you'll want them to look something like:

-(NSMutableArray*)photos
{
    return photos_internal_pointer;
}
-(void)setPhotos:(NSMutableArray*)newValue
{
    [newValue retain];
    [photos_internal_pointer release];
    photos_internal_pointer = newValue;
}

Where photos_internal_pointer is of type NSMutableArray*, of course. This should keep them in sync because setting the pointers equal to each other will have them point to the same object.

Lion Kabob
`@synthesize` creates perfectly fine accessors for a `retain` property; something else is causing the problem here. (Also, your `setPhotos:` method leaks `photos_internal_pointer`.)
andyvn22
Thanks for catching that, edited even though the actual answer will probably be revealed soon.
Lion Kabob
+1  A: 

You're right, those property values are pointers, so there should be only one mutable array here. I agree with andyvn22 that something else is going on, something not evident in the code you've posted so far.

Here's a possible way to debug it: set a breakpoint on each of those NSLogs, and when you drop into the debugger, use the debug window (cmd-shift-Y) to examine the actual memory address of the photos property in each case. There are only two possibilities:

  1. The addresses are the same, in which case it really is the same array, and some code you're not thinking of is mutating the array between the first log and the second; or,

  2. The addresses are different, i.e. they're different arrays, and some code you're not thinking of is reassigning a new array to the child (or parent) photos property.

Good luck!

Joe Strout
Great advice. The culprit here is: "[[self mutableArrayValueForKey:@"photos"] addObject:[photo resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(200.0f, 300.0f) interpolationQuality:kCGInterpolationLow]];" It is changing the instance to point to a new NSMutableArray. I thought this was the same as calling "[self.photos addObject ...] except KVC friendly.
wgpubs
Doing KVO with NSMutuable array is ugly. I just ended up abandoning it as the calls to "mutableArrayValueForKey:" actually end up changing the instance "photos" was pointing too each time it is called.
wgpubs
Having just read the KVC docs on the method, it is clear your message is failing because you haven't fully implemented your one to many property. Look for "Accessor Search Patterns in ORdered Collections" in the KVC Programming Guide: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/SearchImplementation.html#//apple_ref/doc/uid/20000955You need a number of methods such as insertObjectInPhotosAtIndex: and removeObjectFromPhotosAtIndex: to make this work without zapping your mutable array.
JeremyP