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
From the course: Swift 5: Protocol-Oriented Programming
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…
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
-
-
-
-
-
(Locked)
The importance of generics3m 12s
-
(Locked)
Defining generic functions and methods3m 32s
-
(Locked)
Working with generic types3m 29s
-
(Locked)
Placeholder types in protocols1m 54s
-
(Locked)
Using generics with protocols1m 45s
-
(Locked)
Challenge: Implementing a generic stack1m 21s
-
(Locked)
Solution: Implementing a generic stack6m 45s
-
(Locked)
-
-