Ios 需要在解除分配之前移除观察者,但ARC禁止覆盖解除锁定
我有一个类,Ios 需要在解除分配之前移除观察者,但ARC禁止覆盖解除锁定,ios,objective-c,location,observer-pattern,dealloc,Ios,Objective C,Location,Observer Pattern,Dealloc,我有一个类,RA\u CustomCell:UITableViewCell。此类的某些实例注册为另一类中变量currentLocation的观察者 RA_CustomCell.m -(void)awakeFromNib { [self registerAsListener] } -(void)registerAsListener { if ([self.reuseIdentifier isEqualToString:@"locationcell1"]) {
RA\u CustomCell:UITableViewCell
。此类的某些实例注册为另一类中变量currentLocation
的观察者
RA_CustomCell.m
-(void)awakeFromNib
{
[self registerAsListener]
}
-(void)registerAsListener
{
if ([self.reuseIdentifier isEqualToString:@"locationcell1"])
{
[[RA_LocationSingleton locationSingleton]
addObserver:self
forKeyPath:@"currentLocation"
options:NSKeyValueObservingOptionNew
context:nil];
}
}
然而,当用户向后导航时,这些单元格自然会被释放。问题是,当currentLocation变量自身更新时,我会出现以下崩溃错误:
*** -[RA_CustomCell retain]: message sent to deallocated instance 0x9bd9890
不幸的是,我无法覆盖-dealoc
,因为我正在使用ARC,键入[super dealoc]
会产生以下警报:
ARC forbids explicit message send of 'dealloc'
我的问题是,我应该如何最好地管理我的位置侦听器以避免这种崩溃?只需使用dealloc而不调用
[super dealloc]
:
- (void)dealloc {
[[RA_LocationSingleton locationSingleton] removeObserver:self
forKeyPath:@"currentLocation"
context:nil];
}
从苹果公司的文档:
ARC中的自定义dealloc方法不需要调用[super dealloc]
(它实际上会导致编译器错误)。超级链接是
由编译器自动执行
您可以覆盖
dealoc
,只需自动为您调用[super dealoc]
苹果文档对此进行了解释
“ARC中的自定义dealloc方法不需要调用[super dealloc](它实际上会导致编译器错误)。到super的链接是由编译器自动执行的。”