属性属性“保留";似乎没有工作吗? [英] Property attribute "retain" doesn't seem to be working?

查看:111
本文介绍了属性属性“保留";似乎没有工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从许多Apple代码示例之一中实现了一些代码,但是我有点麻烦,因为其中一个属性的keep属性似乎无法正常工作.这是属性声明:

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;

这是代码:

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

我知道,(保留)应该导致保留计数在分配时增加1;但是,除非我自己执行发送[aController保留]或不执行发送[aController发布],否则代码将失败.我在这里想念什么?

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?

推荐答案

引用editingViewController时,它等效于self->editingViewController,即访问ivar.

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

如果要使用getter或setter,则需要使用self.editingViewController或等效的[self setEditingViewController:aController].

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

这就是为什么我更喜欢使用与属性名称不同的ivar的原因,例如:

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;

然后,您可以将懒惰的getter编写为:

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;
}

- (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;
}

我可能会使用前一种方法(不调用设置器),因为editingViewController的值(由任何观察者看到的)并没有真正改变,但是无论哪种方法都可以正常工作,并且使用不同的名称(对于ivar和property) )有助于避免造成混淆或意外误用.还是鼓励使用该属性(因为它避免使用稍微难看的前缀).

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

请注意,Apple保留_前缀,并且init/dealloc例程中不应使用setter和getter.

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

这篇关于属性属性“保留";似乎没有工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆