SkillAgentSearch skills...

SwiftProtocolDelegatePattern

Sample code about Protocol Delegate Pattern

Install / Use

/learn @mkilmerr/SwiftProtocolDelegatePattern
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

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)
    }
}

View on GitHub
GitHub Stars4
CategoryDevelopment
Updated2y ago
Forks0

Languages

Swift

Security Score

55/100

Audited on Jul 17, 2023

No findings