KVO versus NSNotifications [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-13 11:57:41

问题


Is there any advantage to use KVO instead of the more "generic" (and to my opion more robust) feature of NSNotifications ?


回答1:


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.)




回答2:


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.



来源:https://stackoverflow.com/questions/5866502/kvo-versus-nsnotifications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!