为什么在分配保留属性时不应该释放?
这个问题与这个问题相关,但更简单。 [我想我可能已经接近结束这些愚蠢的问题了,可以开始讨论严肃的事情了:)]。
我有一个 retain
属性并像这样设置它:
UINavigationController *thing = [[UINavigationController alloc] initWithRootViewController:one];
// thing's retain count is one
navController = thing;
// thing's retain count is still one!
[thing release];
// now retain count is zero, which is wrong
我无法理解为什么保留计数为零。 navController
定义为
@property (nonatomic, retain) UINavigationController *navController;
该属性不应该将保留计数增加一吗?
This question is related to this one, but simpler. [I think I may be close to the end of these dumb questions and can get down to serious business :)].
I have a retain
property and set to it like this:
UINavigationController *thing = [[UINavigationController alloc] initWithRootViewController:one];
// thing's retain count is one
navController = thing;
// thing's retain count is still one!
[thing release];
// now retain count is zero, which is wrong
I cannot understand why the retain count gets to zero. navController
is defined as
@property (nonatomic, retain) UINavigationController *navController;
Shouldn't the property increase the retain count by one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是:您直接分配给属性的基础实例变量,而不是调用设置器。属性魔法不是这样触发的。尝试
改为(其余代码无需更改)。
The problem is: you are assigning directly to the property's underlying instance variable instead of calling the setter. The property magic is not triggered this way. Try
instead (remaining code needs no change).