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.

Protocol extensions

Protocol extensions

From the course: Swift 5: Protocol-Oriented Programming

Start my 1-month free trial

Protocol extensions

- [Instructor] Providing default behavior in the protocol could save us a lot of time when implementing the conforming types. Although we can't include implementation in the protocol's body, it is possible to provide default method and property implementations in a protocol extension. You can open the project from the Exercise Files folder, Chapter 1, 01-08, begin. Let's provide the default implementations for the TaggedPersistable protocol first. We declare protocol extensions using the extension keyword. It's the same syntax we used to create type extensions. Let me scroll up, and here's our TaggedPersistable protocol. I'm going to put the extension right below it. After creating this protocol extension, I'm going to copy and paste the method definitions from the corresponding MyData extension. This is where it conforms to a TaggedPersistable, so let's copy the initializer and the persist method. Next, we provide default…

Contents