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
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset