KVO 与 NSNotifications
使用 KVO 代替 NSNotification 的更“通用”(并且在我看来更强大)功能有什么优势吗?
Is there any advantage to use KVO instead of the more "generic" (and to my opion more robust) feature of NSNotification
s ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我非常讨厌 KVO,主要是因为它迫使我通过单个处理程序路由所有 KVO 通知。如果可以选择,我会使用任何可用的东西。但 KVO 具有明显的优势,它可用于标准库中的许多类 - 如果您想观察标准库中某些类的属性更改,KVO 可能是您唯一的选择。在 Cocoa Builder 上有一个非常有趣的线程 关于 KVO 以及各种观察和绑定选项。
(我应该补充一点,我只是对 KVO 作为高级观察接口有一些抱怨。我认为它非常适合作为其他技术的管道,尤其是绑定。)
I hate KVO with passion, mainly because it forces me to route all KVO notifications through a single handler. I use whatever else available if I have the choice. But KVO has the distinct advantage of being available for many of the classes in the standard library – if you want to observe property changes on some classes from the standard library, KVO might be your only option. There is one very interesting thread on Cocoa Builder about KVO and various observing and binding options.
(I should add that I only have gripes with KVO as a high-level observation interface. I think it’s very good as plumbing for other technologies, especially bindings.)
为了使用 NSNotifications 做类似 KVO 的事情,您必须为您想要观察的每个属性编写自定义样板访问器方法。
与使用 KVO 相比,这既不那么“通用”,也不那么健壮。
In order to use NSNotifications todo something similar to KVO you would have to write custom boilerplate accessor methods for every property you wanted to observe.
This is both much less "generic" and much less robust than using KVO.