Created
January 23, 2023 17:59
-
-
Save DarrenHurst/69f2643997c44fc54cec948513290fac to your computer and use it in GitHub Desktop.
Optional Binding Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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