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.
Adopting protocols
From the course: Swift 5: Protocol-Oriented Programming
Adopting protocols
- [Instructor] If you want to create a type, confirming to a protocol, we must declare and define the matching properties and methods. To follow along with me, open the project from the exercise files folder, Chapter 1, 01_03, begin. Let's create a structure called tagged data. To make it adopt the taggable protocol, add a column after the type's name, and write the protocol's name. After this change, we get a compiler error that says, "Type 'TaggedData' does not conform to protocol 'Taggable'." We can rely on auto-complete to add the missing property and method declarations. So, let's do that. Click the red dot to reveal the details, then click 'Fix.' The code gets generated. It has a lot of placeholders and, the implementation is missing, so let's add some code. The initializer should initialize the tag and the data properties. Self.tag = tag, and self.data = data. The update method updates the data property and…
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
-
-
-
-
-