Property attribute “retain” doesn't seem to be working?

Property attribute “retain” doesn't seem to be working?

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?

UICachedDeviceWhiteColor and NSAutoRelease for table cell mem leaks


Can I develop an iPhone app using java?
When you reference editingViewController, it is equivalent to self->editingViewController, i.e.

Lock orientation UIWebView on iPhone
an access to an ivar..
How to ALTER sqlite column (iPhone)?
If you want to use a getter or setter, you need to use self.editingViewController, or equivalently [self setEditingViewController:aController]..
How to escape characters in href=mailto (iPhone)
This is why I prefer to use an ivar with a different name to the property, for example:.
iPhone SDK: UIWebView
EditingViewController* i_editingViewController;  @property (nonatomic, retain) EditingViewController *editingViewController;  @synthesize editingViewController = i_editingViewController; 
Then you can write your lazy getter as:.
Focus OnLoad=“document.form_id.form_field_id.focus();” on the iPhone
- (EditingViewController *)editingViewController {     // Instantiate the editing view controller if necessary. 

Getting access to touchesBegan/Moved/Ended to a UITableViewCell
if (i_editingViewController == nil) { i_editingViewController = [[EditingViewController alloc] init]; } return i_editingViewController; }
- (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..


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

66 out of 100 based on 31 user ratings 956 reviews