Protocols

Protocols

A protocol is a collection of methods, properties, and rules that a class, struct, or enum can adopt.

Syntax

A protocol is created with the protocol keyword:

protocol MyProtocol {
// Protocol body
}

Note: Names are written in PascalCase.

For class, structure, or enumeration data types to adopt a protocol, it chains onto their definition and is separated with a colon:

class MyClass: MyProtocol, OtherProtocol, ... {
// This can be a class, structure, or enumeration
}

Property Requirements

Property requirements are variables that indicate a type and the get/set keywords determine if that property is gettable and/or settable.

protocol MyProtocol {
var getAndSet: Int { get set }
var onlyGet: String { get }
}

Note: Gettable variables can be read and settable variables can be set or changed. Programmers sometimes only allow a variable to be gettable because they don’t want it to be mutated.

Data types that adopt a protocol, must conform to the properties and methods defined in it.

protocol Grammar {
var alphabet: String { get }
}
struct Language: Grammar {
var alphabet: String
}
let english = Language(alphabet: "abcdefghijklmnopqrstuvwxyz")
print(english.alphabet)
// Output: abcdefghijklmnopqrstuvwxyz
Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn Swift on Codecademy