From the course: Swift 5: Protocol-Oriented Programming
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
The benefits of thinking in protocols
From the course: Swift 5: Protocol-Oriented Programming
The benefits of thinking in protocols
- [Instructor] Now let's revamp our design using a protocol-oriented approach. I'm going to remove this line to get rid of the compiler error, and let's create a Playground page and name it Protocols. The protocol-oriented approach works by creating a protocol. We're going to forget classes and focus on protocols. We'll create dedicated protocols for each major feature. Let's clean up the generated code, and we'll start with the protocol taggable. It declares two property requirements, tag and data. Both are gettable and settable. Data, that's dot data also get set. The protocol also declares an initializer requirement. It has two parameters, the tag and the data. Conforming types must implement all these properties and the initializer declared in the taggable protocol. Next, I create a protocol that declares the requirements needed to persist the data. Let's name it TaggedPersistable. It declares the initializer requirement…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.