代码之家  ›  专栏  ›  技术社区  ›  Dan Rosenstark

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

  •  0
  • Dan Rosenstark  · 技术社区  · 14 年前

    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;
    

    物业不应该增加一倍的保有量吗?

    1 回复  |  直到 7 年前
        1
  •  7
  •   Dirk    14 年前

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

    self.navController = thing;