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.
The importance of generics
From the course: Swift 5: Protocol-Oriented Programming
The importance of generics
- [Instructor] Although this course is about protocol-oriented programming, we can't avoid talking about generics. Generics are deeply rooted in the language. We usually don't even notice that we're relying on them. Whenever we encounter protocols, we'll often also see generics. As an example, the built-in Swift collection types conform to the collection protocol and several other protocols as well. Besides, they are also implemented as generic types. The dictionary, the array and the set, are all defined as generic types. So, why do we use generics? In short, generics can save us from typing similar code over and over again. I show you what that means through a practical example. Let's assume that we need a function that tests whether two values are equal. We could come up with the following solution. This implementation is straightforward and we can test it by typing equals and for the left hand side argument, I…
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)
-
-