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.

Placeholder types in protocols

Placeholder types in protocols

From the course: Swift 5: Protocol-Oriented Programming

Start my 1-month free trial

Placeholder types in protocols

- So far, we've looked at the benefits of generic types and functions and how to use them. How 'about protocols? I've gone ahead and created the new playground page called Associated Types. I added the definition of the Taggable Protocol. You can find the project in the Exercise Five, chapter three, o three o four, begin further. The Taggable Protocol defines requirements for any tag that can hold binary data and uses a tag to identify it. Now, wouldn't it be great to create a protocol that's not restricted to the data type? Swift allows us to declare a place for the types in protocols. Let's see how. I declare an associated type next. The associated type key word lets us define the place for the name for a type within a protocol. I'm going to call it Content. The actual type is unknown until a confirming type provides it. Next, I replace the type of the data property with a place order type. And finally, let's…

Contents