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

Adopting protocols

From the course: Swift 5: Protocol-Oriented Programming

Start my 1-month free trial

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…

Contents