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

只愿长相守 提交于 2019-12-11 03:05:02

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1215953/property-attribute-retain-doesnt-seem-to-be-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!