Objective-C的析构函数与ARC
问题描述:
我想创造我的Objective-C类的一些清理代码,通过覆盖的dealloc:Objective-C的析构函数与ARC
-(void)dealloc {
//cleanup code
[super dealloc];
}
虽然我不能这样做,因为[super dealloc]
是由编译器时,ARC禁止已启用。有没有其他的方法可以使用?
答
从Transitioning to ARC Release Notes(重点煤矿):
如果你需要管理比释放实例变量的其他资源,您可以实现一个dealloc方法。您不必(实际上不能)释放实例变量,但是您可能需要在系统类和其他未使用ARC编译的代码上调用[systemClassInstance setDelegate:nil]。
ARC 中的自定义dealloc方法不需要调用[super dealloc](它实际上会导致编译器错误)。超链接是由编译器自动执行的。
所以你可以使用ARC时做同样的清理在dealloc
,只是不叫super
。
答
当ARC处于活动状态时,您不需要拨打[super dealloc]
。 ARC会为你做到这一点。或者,您可以使用prepareForDealloc
方法,该方法允许您拨打super
,并在基类中调用dealloc
。