O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Protocol extensions

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     } } 

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required