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
From the course: Swift 5: Protocol-Oriented Programming
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…
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.
Contents
-
-
-
What is protocol-oriented programming?2m 15s
-
(Locked)
Introducing the protocol5m 18s
-
(Locked)
Adopting protocols1m 54s
-
(Locked)
Conforming to protocols via extensions3m 38s
-
(Locked)
Understanding polymorphism4m
-
(Locked)
Protocol inheritance3m 41s
-
(Locked)
Adopting multiple protocols5m 50s
-
(Locked)
Protocol extensions3m 23s
-
(Locked)
Challenge: Removing tight coupling3m 13s
-
(Locked)
Solution: Removing tight coupling8m 33s
-
-
-
-
-