Protocols and Generics
Learn how to use protocols and generic to write powerful functions, structs, classes, and enums that work with many different types.
StartProtocols and Generics
Lesson 1 of 1
- 1Protocols are an important part of the Swift programming language. A protocol is a set of standards that classes, structs, or enums can choose to implement in their own ways. These standards come i…
- 3In the previous exercise, we saw how to create our own protocols. The Swift standard library includes many useful protocols that you can have your structures, classes, or enums conform to. A useful…
- 4A protocol can inherit from another protocol, like a class can inherit from another class. Protocols can even inherit from multiple protocols at the same time! A struct, class, or enum that confo…
- 5When defining a protocol, we might want to provide a default implementation of its properties and functions. This is useful in cases where the implementation of the functions and properties remain …
- 6Generics are an important part of the Swift programming language. They allow us to create functions that make few assumptions about the underlying data types of its arguments and can be used with m…
- 7Classes, structs, and enums can also be generic, which means they can work with many different types. Here’s a simple example: struct InformationWrapper { var information: T } When creating…
- 8So far we have seen protocols that classes or structs can adopt to add more capabilities and perform more operations. However, the protocols we have discussed only work on specific data types. Cons…
- 9We saw in an earlier exercise that protocols can be used as types on some occasions. let favoriteNumber: Int = 7 let length: Double = 423.4 let purchase: String = “chair” let describableThings: […
- 10Congratulations! You’ve completed a pretty long and complex lesson. You learned how to: - Define and conform to custom protocols - Conform to protocols in the Swift standard library - Use protocol…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory