Pass Data in iOS Apps
Notifications vs Delegates vs Closures vs Key-Value Observation
4 min readJan 20, 2023
Prerequisites:
- You will be expected to be aware how to make a Single View Application in Swift, or be able to code in Swift Playgrounds
Terminology:
Closures: A combination of functions and references to the surrounding context
Delegation: A pattern that enables a class to delegate responsibility for some actions to an instance of another class
NotificationCenter: A notification dispatch mechanism that enables the broadcast of information to registered observers
KVO: (Key-Value Observing), is one of the techniques for observing the program state changes available in Objective-C and Swift
Delegation
I’ve previously written an article on delegation which shows that delegates are a great way of passing information between two loosely-coupled objects.
Pros
- All objects conforming to the protocol must implement those methods
- Easy to understand the program flow
- You can have multiple protocols defined by a single object, each with different delegates