NSNotification VS KVO
我觉得我不完全理解 KVO 和 NSNotification 之间的区别...它们似乎很相似... 您能否举一些例子来说明什么时候最好使用一种方法,什么时候最好使用另一种方法?
我不谈论 Bind 和 IB,但我的意思是使用 NSNotificationCenter 或 KVO 在我的代码中以编程方式添加观察者
[self.preferenceController addObserver:self
forKeyPath:@"color"
options:NSKeyValueObservingOptionOld
context:@"Color-change"
];
I feel that i don't fully understand difference between KVO and NSNotification... They seem to be so similar...
Could you make some example showing when is best to use one method and when the other ?
I don't speak about Bind and IB, but i mean add Observer programmatically in my code with NSNotificationCenter or KVO
[self.preferenceController addObserver:self
forKeyPath:@"color"
options:NSKeyValueObservingOptionOld
context:@"Color-change"
];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
KVO 仅适用于值,NSNotification 可用于值更改,但它可以用于任何内容,并且可以承载更大的负载。
例如,只要文件下载完成,您就可以发布 NSNotification,并且 userInfo 可以包含所花费的时间长度、下载的字节数以及文件已保存到的文件系统路径。
KVO only works on values, NSNotification can be used for value changes but it can be used for anything and can carry a much greater payload.
For example, you could have an NSNotification posted whenever a file has finished downloading and the userInfo could contain the length of time it took, the number of bytes downloaded and the filesystem path that the file has been saved to.