Skip to content

Instantly share code, notes, and snippets.

@DarrenHurst
Created January 23, 2023 17:59
Show Gist options
  • Save DarrenHurst/69f2643997c44fc54cec948513290fac to your computer and use it in GitHub Desktop.
Save DarrenHurst/69f2643997c44fc54cec948513290fac to your computer and use it in GitHub Desktop.
Optional Binding Example
protocol UserProtocol {
var user: User {get set}
}
struct Profile : Codable {
var posts: [String] = [ ]
var comments: [String] = [ ]
}
class User : ObservableObject {
@Published var username: String
@Published var password: String
var isAuthenticated: Bool = false
@Published var profile: Profile?
init(username:String, password:String) {
self.username = username
self.password = password
}
@MainActor
func authenticate(user: User) async -> User {
if ( user.username == "admin" && user.password == "password") {
user.isAuthenticated = true
}
return user
}
@MainActor
func getLoggedInUser( user: User ) -> (name:String, isAuthenticated: Bool, profile: Profile?) {
// process Login service and return isAuthenticatied
let loggedInUser: (String, Bool, Profile?) = (username, isAuthenticated, profile)
return loggedInUser
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment