ios KVO vs NSNotification vs 协议/委托?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7864838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
KVO vs NSNotification vs protocol/delegates?
提问by Ankit Srivastava
I have some idea of which to use when but the exact usage is still not clear to me. Can someone explain with example?
我对什么时候使用哪个有了一些想法,但我仍然不清楚确切的用法。有人可以用例子解释一下吗?
回答by deanWombourne
Use a delegate if you want to talk to only one object. For example, a tableView has a delegate - only one object should be responsible for dealing with it.
如果您只想与一个对象交谈,请使用委托。例如,一个 tableView 有一个委托——只有一个对象应该负责处理它。
Use notifications if you want to tell everyone that something has happened. For example in low memory situations a notification is sent telling your app that there has been a memory warning. Because lots of objects in your app might want to lower their memory usage it's a notification.
如果您想告诉所有人发生了某些事情,请使用通知。例如,在内存不足的情况下,会发送通知告诉您的应用程序存在内存警告。因为您的应用程序中的许多对象可能想要降低它们的内存使用量,所以它是一个通知。
I don't think KVO is a good idea at all and try not to use it but, if you want to find out if a property has changed you can listen for changes.
我认为 KVO 根本不是一个好主意,尽量不要使用它,但是,如果您想知道某个属性是否已更改,您可以监听更改。
Hope that helps.
希望有帮助。
回答by jbat100
Use a delegate when there is a "master/slave" relationship (delegate knows about the class and class knows about the delegate), with one class higher up the control hierarchy, and when it is clear that there won't be situations where other elements (mostly UI) will be interested in knowing what the class has to say.
当存在“主/从”关系时使用委托(委托知道类,类知道委托),一个类在控制层次结构中更高,并且当很明显不会出现其他情况时元素(主要是 UI)将有兴趣了解类要说的内容。
Use notifications when the class is not interested in knowing who listens and how many they are, anybody and any number can register for the notifications.
当班级对知道谁在听以及他们有多少人不感兴趣时使用通知,任何人和任何数字都可以注册通知。
KVO is useful to listen "without the class knowing", although of course that's not the case, the class on which KVO is applied does not need to be changed.
KVO 对于“在类不知道的情况下”进行侦听很有用,当然事实并非如此,应用 KVO 的类不需要更改。
回答by NSResponder
Delegation is a design pattern that you use when you want some other object to modify the sender's behavior. Example: terminal windows avoid showing any lines or characters that are clipped by the window's edges, because the terminal window's delegate alters the size of the window to ensure this.
委托是一种设计模式,当您希望某个其他对象修改发件人的行为时,可以使用该模式。示例:终端窗口避免显示被窗口边缘剪裁的任何行或字符,因为终端窗口的委托会更改窗口的大小以确保这一点。
Notification is a pattern to use when you don't need a response. Example: you get a notification that the system is about to go to sleep. The sender of that notification doesn't care what you do about it.
通知是一种在您不需要响应时使用的模式。示例:您收到系统即将进入睡眠状态的通知。该通知的发送者并不关心您对此做了什么。
回答by user523234
Even when all three would serve your need in a situation, delegate would still be a prefer option:
即使在某种情况下这三个都可以满足您的需求,delegate 仍然是一个首选选项:
- Reuseability.
- Self documented. By examining the class's header file, one would immediately recognize what / how the data exchanged taking places.
- 可重用性。
- 自我记录。通过检查类的头文件,人们会立即识别出数据交换的内容/方式。
回答by MANN
In my opinion KVO is better because of it's zero-overhead advantages. Notifications has overhead even if you aren't using/observing them. To improve that you can use different NotificationCenters but even with that some overhead will be there (correct me if I'm wrong). KVO is little complex but its worth when you have to observe lots of stuff.
在我看来,KVO 更好,因为它的零开销优势。即使您没有使用/观察通知,通知也会产生开销。为了改进您可以使用不同的 NotificationCenters 但即使有一些开销也会有(如果我错了,请纠正我)。KVO 并不复杂,但是当你必须观察很多东西时它是值得的。