March 2020
Intermediate to advanced
392 pages
10h 10m
English
Protocols are a powerful way to ensure your application is consistent in structure. They enforce that a class or struct has specific variables and functions available. Your application logic can then rely on that, having the defined functions and variables available. Instead of requiring a class type, you can also require just the protocol and it will accept any applicable class.
Let's take a look at an example:
protocol HasEmail { var email: String { get set }}struct User: Model, HasEmail { var email: String}
We have a HasEmail protocol, which is defining an email variable. Every type (class, enum, or struct) that is now implementing this protocol will need to define such a variable. You can see that User is doing that. ...
Read now
Unlock full access