// 我正在尝试使用@Binding 将@AppStorage 的值传递给类。// @AppStorge 的声明,这是唯一的事实来源。// 我在 SigninViewModel = Class 'SigninViewModel' has no initializers 上收到以下错误
import SwiftUI
struct MainView: View {
@AppStorage("userSignIn") var userSignIn = false
var body: some View {
ZStack {
TabView {
AppointmentsView()
.tabItem {
Image(systemName: "calendar")
Text("Appointments")
}
AccountView(userSignIn: .constant(true))
.tabItem {
Image(systemName: "person")
Text("Profile")
}
StatsView()
.tabItem {
Image(systemName: "chart.bar")
Text("Stats")
}
}
}
}
}
// 这个带有@Binding 的类正在被声明,并且我试图在其中访问@AppStorae 值。
class SigninViewModel: ObservableObject {
@Published var nonce = ""
@Binding var userSignIn: Bool
func authenticate(credential: ASAuthorizationAppleIDCredential) {
// getting Token...
guard let token = credential.identityToken else {
print("Error with Firebase")
return
}
//Token String...
guard let tokenString = String(data: token, encoding: .utf8) else {
print("Error with Token")
return
}
let firebaseCredential = OAuthProvider.credential(withProviderID: "apple.com",
idToken: tokenString,
rawNonce: nonce)
Auth.auth().signIn(with: firebaseCredential) { (result, err) in
if let error = err {
print(error.localizedDescription)
return
}
// User succesfully logged into Firebase...
print("Logged in Success")
// Directing user to Main page...
withAnimation(.easeInOut) {
self.userSignIn = true
}
}
}
}