-
Book Overview & Buying
-
Table Of Contents
Mastering Swift 6 - Seventh Edition
By :
Even though protocols do not include any functionality, they’re regarded as full-fledged types in Swift and can be used just like any other type. This means we can use protocols as parameter types or return types in functions. They can also serve as the type for variables, constants, and collections.
Let’s look at some examples to illustrate what that means. For these few examples, we will use the following Person protocol:
protocol Person {
var firstName: String { get set }
var lastName: String { get set }
var birthDate: Date { get set }
var profession: String { get }
init(firstName: String,lastName: String, birthDate: Date)
}
The Person protocol requires conforming types to have four properties: firstName, lastName, and birthDate (all of which must be both readable and writable ({ get set })), and profession, which is read-only ({ get }). Additionally, the protocol requires types that conform to this protocol...