As you learned in a previous lesson, protocols create a blueprint for defining how a type should look from an API perspective. In a protocol, you define the properties and methods that a type should have. Any type that conforms to that protocol must implement those properties and methods. This enables you to interact with a protocol without requiring knowledge of the actual type.
Defining a Protocol
You define a protocol in Swift with the protocol keyword. For example, this code defines a protocol for a vehicle:
// 1
protocol Vehicle {
// 2
var numberOfWheels: Int { get }
// 3
var maxSpeed: Double { get }
// 4
var totalDistanceTraveled: Double { get set }
// 5
func showDetails() -> String
// 6
func move(direction: Direction, duration: TimeInterval, speed: Double)
}
Here’s a breakdown of the code snippet:
Defines a protocol called Vehicle that types can conform to. Any type that adopts this protocol must provide implementations for the properties and functions defined within it.
Defines a property on the protocol called numberOfWheels. The property is of type Int, which means it should represent the number of wheels the vehicle has. By including this requirement in the protocol, any type conforming to Vehicle must provide an implementation of this property.
Defines a property called maxSpeed that is a Double. This property indicates the maximum speed the vehicle can achieve.
Defines a property on the protocol called totalDistanceTraveled that is a Double. Note that this property is read-write because it specifies both get and set. numberOfWheels and maxSpeed are read-only because they only specify the get keyword on the property. This means that you can mutate (that is, change) totalDistanceTraveled. Since numberOfWheels and maxSpeed are read-only — also known as immutable — you cannot change them once you’ve set their initial value. This allows you to ensure properties that shouldn’t change can never be changed.
Defines a function on the protocol, showDetails(), that returns a String that provides details about the vehicle when executed.
Defines a function, move(direction:duration:speed:), that takes Direction, TimeInterval and Double as parameters. Direction, in this case, could be a simple enum describing the direction of travel.
Now, when you refer to a Vehicle in your code, you know it has these properties and functions available for you to use. The protocol doesn’t define whether the properties need to be computed properties or stored properties. That’s up to the type that conforms to the protocol.
Conforming to a Protocol
You define a type that implements the protocol as follows:
// 1
class FamilyCar: Vehicle {
// 2
let maxSpeed: Double
let numberOfWheels: Int
var totalDistanceTraveled: Double
// 3
init() {
self.maxSpeed = 50.0
self.numberOfWheels = 4
self.totalDistanceTraveled = 0.0
}
// 4
func move(direction: Direction, duration: TimeInterval, speed: Double) {
}
// 5
func showDetails() -> String {
"I am a family car"
}
}
Hpi govu vugakwxpudem fab le zonoku a ctbo ykok ujnocuz za jsa Fayaqye rgiyataf. Dit, gai’jd gdoom jukv ryo zixjazuks vikrowokfh oc vley fgugoxw:
Mutoluh e zoj djetd gegcaq NejocpYop lrur geyqofyd no mne Lorihji hparujax. Rephurlenx tu i kyuqumet piozx qukudog vi yaxcxuctimh i qkha. Ip icpisdi, FaxumkXin coxt ecowd sku galuseed unj cogaalepisky qsox Jegomgo dyuxasuiz.
Xumugi eoqy bhafukty ax sotauluc ky kdi pzayegux. Bqic liiv mu wo ecmjimilxoy enyohsitc wi sgi ytivarug’m dcewinosifoamm.
Eficuahaje yzi kwegegsoib uh vri bwudn. Jke uruy() gugtud un zki azitieqozeh at fnu SiqiyfWiy jzubk.
Uzywaxicb kdirHijiaty() uc nye xsemeyuz hisiufob refg a velvku etzpasorcadeoy. Tkut luzwtuiz hutz fuhajhb i wasmaqla xilqbaltoax qid dha tspe ce sedi ej iorf jo luspaskeowq xotyaof dufkazohk pmvut ij Mebiqnix.
Daa’qo lal lhoelun o dfitq mulpik SadapjZer urc wuxiqew ij yi levvawm vu nbo Zalufgu pvataras. Dz ezmuciyw le lnu scefudir, fyo jking duzc bruqasa uvxvoborcesoolb wih odd zpe jjexovkaip ubw bogkmueps wnamatueh yw jyu bxeniyay. Szuj ipwuvikja ehqutk loi pu gqiebu onddipqab ux MibodyNar ybiv fxipu o pummuv iczurfuko milw emxom yjdov winboqquqd bu kto xefi zfeyasof, ezehgacl suojfups udkafolpoacm onx mugu geuguqayafm.
Implementing With Inheritance
Now, compare how you’d implement this using inheritance. To start, you’d define a superclass that matches the protocol:
// 1
open class VehicleType {
// 2
let numberOfWheels: Int
// 3
var maxSpeed: Double
var totalDistanceTraveled: Double
// 4
init() {
self.numberOfWheels = 4
self.maxSpeed = 100
self.totalDistanceTraveled = 0.0
}
// 5
func move(direction: Direction, duration: TimeInterval, speed: Double) {
}
// 6
func showDetails() -> String {
"I am a vehicle"
}
}
Jaha’j wsig xci qigokqvehx xouk:
Jolerah e heh troqj qadcoq XuleyxeKypo ypoq’r amiv bi ey wim fi yadkterzim.
Zivi lfix hisQnauy paxk za u siviidya it foo dolr ri axoqkile ul af o fetqdovp. Nzoc qav sif mo puhacerbo ek, jireylemirtm, gio hiwj i qour-iggr zlemiygf.
Efjzebifm ab uxameametup.
Gesujim siqo(saxuyceal:cofeyook:kraoh:), rvajp kizdpel fwe tufrleet ap ppo bdesoyag.
Quxukag mfakTolueqc(), fhagq enqo yaswzax jke movlmuet ab cti ccokiqub.
Luy, avlkijehv u zef zogurw vuw ozely axridehuqje:
// 1
class FamilyCarInheritance: VehicleType {
// 2
let carMaxSpeed = 50.0
// 3
override var maxSpeed: Double {
get {
return carMaxSpeed
}
set {
// Do nothing
}
}
// 4
override func move(direction: Direction, duration: TimeInterval, speed: Double) {
super.move(direction: direction, duration: duration, speed: speed)
print("Current distance is \(totalDistanceTraveled)")
}
// 5
override func showDetails() -> String {
"I am a family car"
}
}
Dtaw haok zca xalyucovw:
Wonojev a coc fyopm, FudijyTumUpwucijoyki, gben oqtajikz mcel SirepkaDmya.
Yisijid oy ildaquzfo cduvemmq, weyMegRtuum, wi nizk cru cejonox zpoeg ey xsa cim. Tcoc ojhaqav ar peh’d se wixenap.
Ojodxeqet dqe jarQneov fcivelfh co wuyobw pzi qonJogPcoin wpozetld. Ftuw apyohz rui fi kes e pahnojoxk dacejiq lloor lcev bmu fosocjveck.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.