SwiftProtocolDelegatePattern
Sample code about Protocol Delegate Pattern
Install / Use
/learn @mkilmerr/SwiftProtocolDelegatePatternREADME
Swift Protocol Delegate Pattern :metal:
Sample code about Protocol Delegate Pattern
What is a Protocol?
Protocols defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements.
What is a Delegate?
Delegates are a design pattern that allows one object to send messages to another object when a specific event happens
Protocol and Delegate in this Example
Protocol
protocol SettingsViewControllerDelegate{
func didChange(_ profile:Profile)
}
Create SettingsViewControllerDelegate variable
var settingsViewControllerDelegate:SettingsViewControllerDelegate?
Call Delegate ( when Save Bar Button Item Tapped )
settingsViewControllerDelegate?.didChange(profile)
Assigning the delegate to Settings ViewController
controller.settingsViewControllerDelegate = self
Implementing the Protocol methods
extension ProfileViewController:SettingsViewControllerDelegate{
func didChange(_ profile: Profile) {
self.apply(profile)
}
}
