gpt4 book ai didi

objective-c - KVO 说 KVO 观察者已注册,即使它没有(或者是吗?)

转载 作者:行者123 更新时间:2023-12-03 17:44:57 25 4
gpt4 key购买 nike

当我的应用程序关闭时,主 Controller 类将其自身作为观察者从模型中删除,然后释放模型。像这样:

- (void)dealloc {
[theModel removeObserver:self
forKeyPath:@"myValue"];
[theModel release];
[super dealloc];
}

紧接着,调试器说:

2010-04-29 14:07:40.294 MyProgram[13678:a0f] An instance 0x116f2e880 of class TheModel was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0x100288450> (
<NSKeyValueObservance 0x1002aca90: Observer: 0x116f40ec0, Key path: myValue, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x116f80430>
)

其中 0x116f2e880 确实是模型,0x116f40ec0 确实是 Controller 。

当 Controller 将自己从观察者身份中删除时,它怎么仍然是观察者?

最佳答案

这只是一个猜测,但是您是否多次将 Controller 添加为该关键路径的观察者?也许有不同的选择或背景?或者您是否将其作为观察者添加到不同的对象,但具有解析为模型对象的关键路径?例如如果 foo 有属性 theModel 并且您使用键路径“theModel.myValue”将 Controller 添加为 foo 的观察者。

您能否在 -addObserver:forKeyPath:options:context: 上放置一个断点,看看添加了什么以及何时添加?

关于objective-c - KVO 说 KVO 观察者已注册,即使它没有(或者是吗?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2739763/

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