Protocols and Generics
Lesson 1 of 1
  1. 1
    Protocols 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…
  2. 2
    A protocol defines a blueprint or a set of requirements that are needed to accomplish a particular task. Classes, structs, and enums can conform to protocols by providing an implementation of the p…
  3. 3
    In 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…
  4. 4
    A 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…
  5. 5
    When 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 …
  6. 6
    Generics 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…
  7. 7
    Classes, 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…
  8. 8
    So 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…
  9. 9
    We 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: […
  10. 10
    Congratulations! 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