Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KVO vs NSNotification vs protocol/delegates?

I have some idea of which to use when but the exact usage is still not clear to me. Can someone explain with example?

like image 879
Ankit Srivastava Avatar asked Oct 23 '11 07:10

Ankit Srivastava


People also ask

What's different between delegate and KVO?

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. Show activity on this post. Delegation is a design pattern that you use when you want some other object to modify the sender's behavior.

What is difference between protocol and delegate in Swift?

We can say Protocol as a set of rules. That rules can be optional or required like we have to use in protocol. Delegates is a message passing technique in objective C and swift. An object has to take care of that message.

What is difference between notification and delegate in Swift?

Delegate is passing message from one object to other object. It is like one to one communication while nsnotification is like passing message to multiple objects at the same time.

What is a delegate in swift programming?

The Delegate Pattern in Swift In Swift, a delegate is a controller object with a defined interface that can be used to control or modify the behavior of another object. One example is the UIApplicaitonDelegate in an iOS app.


1 Answers

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

like image 88
deanWombourne Avatar answered Sep 23 '22 06:09

deanWombourne