views:

170

answers:

2

I've implemented a bit of code from one of the many Apple code examples, but I'm having a bit of trouble, because the retain attribute of one of the properties doesn't appear to be working. Here's the property declaration:

@property (nonatomic, retain) EditingViewController *editingViewController;

And here's the code:

- (EditingViewController *)editingViewController {
    // Instantiate the editing view controller if necessary.
    if (editingViewController == nil) {
        EditingViewController *aController = [[EditingViewController alloc] init];
        editingViewController = aController;
        [aController release];
    }
    return editingViewController;
}

I understand that (retain) is supposed to cause the retain count to increase by 1 on assignment; however, the code fails unless I do send [aController retain] myself, or don't send [aController release]. What am I missing here?

A: 

You have to write self.editingViewController in order to use the property. Just "editingViewController" is a direct access to the Class member variable, whereas self.editingViewController is equivalent to [self setEditingViewController:...] and will do the appropriate retain/release job.

squelart
+4  A: 

When you reference editingViewController, it is equivalent to self->editingViewController, i.e. an access to an ivar.

If you want to use a getter or setter, you need to use self.editingViewController, or equivalently [self setEditingViewController:aController].

This is why I prefer to use an ivar with a different name to the property, for example:

EditingViewController* i_editingViewController;

@property (nonatomic, retain) EditingViewController *editingViewController;

@synthesize editingViewController = i_editingViewController;

Then you can write your lazy getter as:

- (EditingViewController *)editingViewController {
    // Instantiate the editing view controller if necessary.
    if (i_editingViewController == nil) {
        i_editingViewController = [[EditingViewController alloc] init];
    }
    return i_editingViewController;
}

or

- (EditingViewController *)editingViewController {
    // Instantiate the editing view controller if necessary.
    if (i_editingViewController == nil) {
        EditingViewController *aController = [[EditingViewController alloc] init];
        self.editingViewController = aController;
        [aController release];
    }
    return i_editingViewController;
}

I would probably use the former method (not invoking the setter) because the value of editingViewController (as seen by any observer) has not really changed, but either way should work fine and the different name (for ivar and property) help avoid the confusion or accidental misused. It is also a mild encouragement to use the property (since it avoids the slightly ugly prefix).

Note that Apple reserves the _ prefix, and that setters and getters should not be used in the init/dealloc routines.

Peter N Lewis
The code example really helped me to see the error of my ways. When I was troubleshooting this, I was trying to use self.editingViewController on all three instances, which of course was recursively calling tableView:editingViewController for two of them. Duh.
JoBu1324