特性属性“retain” 似乎不起作用?
我已经从众多 Apple 代码示例之一中实现了一些代码,但遇到了一些麻烦,因为其中一个属性的保留属性似乎不起作用。 这是属性声明:
@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;
}
我理解 (retain) 应该会导致赋值时保留计数增加 1; 但是,除非我自己做发送[aController keep],或者不发送[aController release],否则代码会失败。 我在这里缺少什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您引用
editingViewController
时,它相当于self->editingViewController
,即对ivar的访问。如果您想使用 getter 或 setter,则需要使用
self.editingViewController
或等效的[self setEditingViewController:aController]
。这就是为什么我更喜欢使用与属性名称不同的 ivar,例如:
然后你可以将惰性 getter 编写为:
或者
我可能会使用前一种方法(不调用 setter),因为
的值editViewController
(任何观察者都可以看到)并没有真正改变,但无论哪种方式都应该可以正常工作,并且不同的名称(对于 ivar 和属性)有助于避免混淆或意外误用。 这也是对使用该属性的温和鼓励(因为它避免了稍微难看的前缀)。请注意,Apple 保留 _ 前缀,并且 setter 和 getter 不应在
init
/dealloc
例程中使用。When you reference
editingViewController
, it is equivalent toself->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:
Then you can write your lazy getter as:
or
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.您必须编写 self.editingViewController 才能使用该属性。 只是“
editingViewController
”是对 Class 成员变量的直接访问,而self.editingViewController
相当于 [self setEditingViewController:...] 并将执行适当的保留/释放工作。You have to write
self.editingViewController
in order to use the property. Just "editingViewController
" is a direct access to the Class member variable, whereasself.editingViewController
is equivalent to [self setEditingViewController:...] and will do the appropriate retain/release job.