Protocol Oriented Programming in Swift

Drawbacks of OOP:

1. When you subclass, you have to inherit all properties and methods which you may not need. Your object unnecessarily bloated.

Protocols

We need to apply all methods and properties to the related struct/class.

Protocol Extensions

We don’t need to use override keyword to add extra functionality.

Protocols as a Type

Practical usage: Combine UILabel, UIImageView and loop through to change color.

Delegate

Delegates allows us to customize an object’s behavior and to be notified about certain events. Only requirement is a protocol to communicate requirements between classes. (It doesn’t require the delegating class to know anything at all about a delegate — only that it conforms to a protocol.)

  • Lightweight
  • Flexible
  1. Desing sender
  2. Desing receiver and implementation
  3. Assign delegate
Designing protocol
Designing sender (not conforms to PassDataDelegate)
Designing receiver and implementation in it
Creating objects and assigning delegate to related VC

Data Source

Briefly, communicating from receiver to sender (backward)

message returns an optional

Software engineer @huawei , musician

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store