NSNotification VS KVO [英] NSNotification VS KVO

查看:111
本文介绍了NSNotification VS KVO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我不完全理解KVO和NSNotification之间的区别...他们似乎是这么相似...
你能举一个例子显示什么时候最好使用一种方法,而当其他?

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 ?

我不提及Bind和IB,但我的意思是在NSNotificationCenter或KVO的程式码中加入Observer

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"
];


推荐答案

KVO仅适用于值,NSNotification可用于值变化,但它可以用于任何东西,并可以携带更大的有效载荷。

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.

例如,您可以有一个NSNotification发布每当文件完成下载和userInfo可以包含所需的时间长度,下载的字节数以及文件已保存到的文件系统路径。

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.

这篇关于NSNotification VS KVO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆