gpt4 book ai didi

ios - 释放前需要removeObserver,但是ARC禁止覆盖dealloc

转载 作者:行者123 更新时间:2023-12-01 18:57:33 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





KVO and ARC how to removeObserver

(3 个回答)


7年前关闭。




我有课,RA_CustomCell : UITableViewCell .此类的一些实例注册为变量 currentLocation 的观察者。在另一个类(class)RA_LocationSingleton .

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

不幸的是,我无法覆盖 -dealloc因为我正在使用 ARC,并输入 [super dealloc]产生以下警报:
ARC forbids explicit message send of 'dealloc'

我的问题是,我应该如何最好地管理我的位置监听器以避免这种崩溃?

最佳答案

只需使用 dealloc 而无需调用 [super dealloc] :

- (void)dealloc {
[[RA_LocationSingleton locationSingleton] removeObserver:self
forKeyPath:@"currentLocation"
context:nil];
}

来自 Transitioning to ARC Release Notes, ARC Enforces New Rules 上的苹果文档:

Custom dealloc methods in ARC do not require a call to [super dealloc] (it actually results in a compiler error). The chaining to super is automated and enforced by the compiler.

关于ios - 释放前需要removeObserver,但是ARC禁止覆盖dealloc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25604918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com