Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexvbush/6614a198b409ce462eb0ce93b5d875f8 to your computer and use it in GitHub Desktop.
Save alexvbush/6614a198b409ce462eb0ce93b5d875f8 to your computer and use it in GitHub Desktop.
import Foundation
protocol User {
var firstName: String { get }
var lastName: String { get }
func fullName() -> String
}
class CurrentUser: User {
let firstName: String
let lastName: String
init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
func fullName() -> String {
return "\(firstName) \(lastName)"
}
}
class GuestUser: User {
var firstName = "Guest"
var lastName = "User"
func fullName() -> String {
return "\(firstName) \(lastName)"
}
}
class YourViewControllerOrSomeOtherPlace {
private let user: User
init(user: User) {
self.user = user
}
func viewDidLoadOrSomeOtherMethod() {
print("rendering UI for the user since we clearly have one, either current or guest")
print("user's full name is: \(user.fullName())")
}
}
let currentUser = CurrentUser(firstName: "Joe", lastName: "Dow")
let aVCWithAUser = YourViewControllerOrSomeOtherPlace(user: currentUser)
aVCWithAUser.viewDidLoadOrSomeOtherMethod()
print("=================")
let guestUser = GuestUser()
let aVCWithGuestUser = YourViewControllerOrSomeOtherPlace(user: guestUser)
aVCWithGuestUser.viewDidLoadOrSomeOtherMethod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment