为什么在释放物体后使用'nil'

sus*_*tha 0 release objective-c

有时我在objective-c中看到了这种类型的编码.为什么在释放后将_imageView设置为nil.

 - (void)dealloc {
    [_imageView release];
    _imageView = nil;
    [super dealloc];
 }
Run Code Online (Sandbox Code Playgroud)

pax*_*blo 5

这是为了确保您不会尝试重用刚刚发布的对象.

通过将其设置为nil,您可以确保将来的任何消息都被抛弃(标准nil对象的行为)而不是导致问题.