【问题标题】:Why shouldn't I release when assigning to a retain property?分配给保留属性时为什么不应该释放?
【发布时间】:2010-06-20 23:15:38
【问题描述】:

这个问题和this one有关,但是比较简单。 [我想我可能已经接近完成这些愚蠢的问题,可以开始认真对待了:)]。

我有一个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;

该属性不应该将保留计数加一吗?

【问题讨论】:

    标签: objective-c retaincount


    【解决方案1】:

    问题是:您直接分配给属性的底层实例变量,而不是调用 setter。属性魔法不会以这种方式触发。试试

    self.navController = thing;
    

    改为(其余代码无需更改)。

    【讨论】:

    • 酷,现在我终于可以做到这一点self.navController = [[[UINavigationController alloc] initWithRootViewController:one] autorelease];,这就是我想要的。再次感谢。
    猜你喜欢
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多