I have some idea of which to use when but the exact usage is still not clear to me. Can someone explain with example?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With