April 2017
Intermediate to advanced
316 pages
9h 33m
English
Protocol extensions are among the most important parts of the POP paradigm. They allow us to add functionality to all types that conform to a given protocol. Without protocol extensions, if we had common functionality that was necessary for all types that conformed to a particular protocol, then we would need to add that functionality to each type. This would lead to large amounts of duplicated code.
The following example extends our protocol by adding a logout method and its implementation; thus, any struct, enum, or class that conforms to UserProtocol will have the logout functionality:
extension UserProtocol { func logout(userName: String) -> Bool { return true } }
Read now
Unlock full access