分配到保留属性时为什么不释放?

Dan*_*ark 0 objective-c retaincount

这个问题关系到这一个,但更简单.[我想我可能接近这些愚蠢问题的结尾,可以开始认真的事业:)].

我有一个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
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么保留计数变为零.navController被定义为

@property (nonatomic, retain) UINavigationController *navController;
Run Code Online (Sandbox Code Playgroud)

财产不应该将保留计数增加一个吗?

Dir*_*irk 7

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

self.navController = thing;
Run Code Online (Sandbox Code Playgroud)

相反(剩下的代码不需要改变).