gpt4 book ai didi

objective-c - removeObserver 崩溃 :forKeyPath: in Foundation

转载 作者:太空狗 更新时间:2023-10-30 03:13:45 25 4
gpt4 key购买 nike

我在从 Xcode 的“崩溃”部分检索到以下崩溃日志时遇到了一些问题。只有少数设备受到此崩溃报告的影响。

我已经分析了问题,但我猜这是 Apple 框架上的错误。但我找不到复制它的方法。

这里有一个类似的讨论:Help with crash in removeObserver:forKeyPath: .

有什么提示吗?

Thread 0 name: Thread 0 Crashed:

0 Foundation
0x23507591 _NSKeyValueReplaceObservationInfoForObject + 69(NSKeyValueObserving.m:1166)

1 Foundation
0x23506fe7 -[NSObject(NSKeyValueObserverRegistration)_removeObserver:forProperty:] + 327 (NSKeyValueObserving.m:1552)

2 Foundation
0x23506b03 -[NSObject(NSKeyValueObserverRegistration) removeObserver:forKeyPath:] + 163 (NSKeyValueObserving.m:1696)

3 Foundation
0x235069a7 -[NSObject(NSKeyValueObserverRegistration)removeObserver:forKeyPath:context:] + 219 (NSKeyValueObserving.m:1663)

4 ApplicationName0x0002e233 -[Supervisor removeObjectObserver:forKeyPath:] + 115 (Supervisor.m:344)

removeObjectObserver:forKeyPath:

- (void) removeObjectObserver:(id)object forKeyPath:(NSString *)keyPath { 

@try {
[object removeObserver:self forKeyPath:keyPath context:PrivateKVOContext];

} @catch (NSException *exception) { }
}

最佳答案

Objective-C 中的

Observers 必须格外注意使用:不要将相同的观察者倍数时间添加到同一对象的属性中,如果有则包装移除是一个:

  if ([self observationInfo]) {
@try {
[self removeObserver:self forKeyPath:keyPath];
}
@catch (NSException *exception) {}
}

您遇到崩溃是因为您尝试删除两次观察者,或者您正在删除一个不存在的观察者。

你应该这样添加观察者:

[yourObject addObserver:self forKeyPath:keypath options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionInitial context:nil/yourContext];

编辑:您可能会删除已解除分配的对象上的观察者,从而导致此崩溃。

  if (object && [self observationInfo]) {
@try {
[self removeObserver:self forKeyPath:keyPath];
}
@catch (NSException *exception) {}
}

关于objective-c - removeObserver 崩溃 :forKeyPath: in Foundation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30165085/

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