Understanding Swift Protocol-Oriented Programming

Wayne Bishop
Swift Algorithms & Data Structures
4 min readApr 1, 2020

--

When helping iOS developers prepare for technical interviews I like to put a focus on popular “gotchas” most likely to be asked by hiring managers. Beyond essential data structures and algorithms, a good way to test one’s competency is to present challenges that showcase Swift’s unique features. In addition to the use of optionals and generics, what makes Swift a robust language is its extensive use of protocols. In this essay, we’ll walk through an example of protocol-oriented programming.

Boats, Planes & Seaplanes
To succeed in a technical interview, one must be proficient enough with one’s language of choice to express one’s solutions with specific syntax and/or object design. For our code challenge, we are being asked to “merge” functionality of a Boat and Plane type to make a new Seaplane:

To solve our problem we can assume our objects exist as regular Swift types (eg. class or struct). Seen below, our objects include some basic methods and properties. What should be obvious is that each object implements actions specific to that type. For example, a plane can “fly” while a boat “floats”:

class Boat {    
var hull: String?
var engine: String?
func float(){
print(“I am now floating..”)
}
}
class Plane {…

--

--