Conforming to a protocol

The following code presents an example of protocol conformance with associated type usage:

enum MobileAppUserType: MobileAppUserProtocol { 
case admin
case endUser

func greet(name: String) -> String {
switch self {
case .admin:
return "Welcome (name) - You are Admin"
case .endUser:
return "Welcome (name)!"
}
}
func login(username: String, password: String) -> Bool {
return true
}
func listSelectedModules() -> [String] {
return ["Accounting", "CRM"]
}
}

Then we can create a new mobile user as follows:

let mobileUser: MobileAppUserType = MobileAppUserType.admin 
mobileUser.logout(userName: "Su Tamina")

mobileUser.listSelectedModules()

POP minimizes the inheritance and subclassing necessities by enabling us to conform to protocols and extend them with default implementations.

..................Content has been hidden....................

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