All about protocols in swift

Protocol oriented programming , like functional programming is an important concept in swift . I am learning protocols in swift and I am documenting it here. Everything I know about protocols will be here in this article. Do read and update your knowledge.

Protocol Syntax

protocol syntax
struct adopting multiple protocols
Classes with superclass
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { }
class ViewController: UIViewController {}extension ViewController: UITableViewDataSource, UITableViewDelegate
{
//implement protocol methods ands variables here..
}

Adding Property Requirements

get-set and get
type properties in a protocol declaration

What does conforming to a protocol means?

simple protocol conformation
protocol conformation as computed property

Method Requirements

Struct conforming to a protocol having methods
class conforming to a protocol having methods

Protocols with mutating methods

Struct with mutation functions

Coming back to mutating method requirements:

enum and class implementing a protocol with mutating function

Initializer Requirements

protocol with initializers

Class Implementations of Protocol Initializer Requirements

class conforming to protocol initializers
protocol SomeProtocol {init()}class SomeSuperClass {init() {// initializer implementation goes here}}class SomeSubClass: SomeSuperClass, SomeProtocol {// "required" from SomeProtocol conformance; "override" from SomeSuperClassrequired override init() {// initializer implementation goes here}}

Failable Initializer Requirements

Protocols as Types

Delegation

delegation callback

Adding Protocol Conformance with an Extension

Adding Protocol Conformance with an Extension

Declaring Protocol Adoption with an Extension

Declaring Protocol Adoption with an Extension

Collections of Protocol Types

Collections of Protocol Types

Protocol Inheritance

protocol inheritance

Class-Only Protocols

Class-Only Protocols
error: non-class type ‘XXXXXXXX’ cannot conform to class protocol “YYYYYY.”

Protocol Composition

Protocol Composition swift 4

Checking for Protocol Conformance

Optional Protocol Requirements

optional Protocol Requirements

Protocol Extensions

Protocol extensions

Providing Default Implementations using protocol Extensions

Providing Default Implementations using protocol Extensions

Adding Constraints to Protocol Extensions

Adding Constraints to Protocol Extensions

If you enjoyed reading this post, please share and recommend it so others can find it 💚💚💚💚💚💚 !!!!

iOS and tvOS developer, dreamer, photographer 🤨