KVO vs NSNotification vs protocol/delegates?

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.

Up vote 2 down vote favorite 3 share g+ share fb share tw.

Though I have some idea which to use when but the exact usage is still not clear to me. Can someone explain with example...? Thanks. Iphone ios protocols key-value-observing nsnotifications link|improve this question asked Oct 23 '11 at 7:06Ankit Srivastava3,0531314 93% accept rate.

1 Try reading this, it's a really interesting blog article about the subject for a coders point of view. Blog.shinetech.com/2011/06/14/… – Daniel Oct 23 '11 at 9:44.

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

Hope that helps. PS This sums up why I think KVO is broken.

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

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions