0

在苹果的 <内存管理> 文档中谈到 Outlets 时。

它说

在您的自定义视图控制器类中,您可以实现 viewDidUnload 以调用您的访问器方法将出口设置为零。

我可以理解这一点,因为在这种情况下调用访问器方法来设置 nil 将释放对象并将指针设置为 nil 可以防止访问可能导致崩溃的无效点。

但在那之后,它说:

注意:在 3.0 之前的 iOS 上,viewDidUnload 方法不可用。相反,您应该在 setView: 中将 outlets 设置为 nil,如下例所示:

- (void)setView:(UIView *)aView {
    if (!aView) { // View is being set to nil.
        // Set outlets to nil, e.g.
        self.anOutlet = nil;
    }
    // Invoke super's implementation last.
    [super setView:aView];
}

另外,由于UIViewController中dealloc实现的一个细节,dealloc中还需要设置outlet变量为nil:

- (void)dealloc {
    // Release outlets and set outlet variables to nil.
    [anOutlet release], anOutlet = nil;
    [super dealloc];
}

1)为什么即使在dealloc中我们也需要设置nil?(我认为 dealloc 是对象生命周期的最后一步,没有其他人可以通过这个对象访问出口。)

2)iOS 3.0 或更高版本还需要设置 nil 吗?(我发现 Xcode 自动生成的代码不会将 nil 设置为出口变量,只释放它们。)

4

1 回答 1

2

不,您不需要将您的出口设置为 nil in dealloc。只要确保你释放它们。

于 2011-04-14T06:58:54.783 回答