I am very very new to swift development, and I'm trying to follow multiple different online tutorials to try and build a sign in page. Unfortunately all of the different views and models haven't slotted together well, and I'm having some issues.
The code below seems to work, apart from the ErrorView displaying. This code doesn't compile when the variables alert and error are @Published variable. It gives me the error ``Failed to produce diagnostics for expression". When I change them both to @State variables they don't update the view.
class AppViewModel: ObservableObject{
@Published var signedIn = false
@Published var alert = false;
@Published var error = ""
let auth = Auth.auth();
func signIn(email: String, password: String){
auth.signIn(withEmail: email, password: password) { [weak self] result, error in
guard result != nil, error == nil else{
self?.error = error!.localizedDescription
print(error!.localizedDescription)
self?.alert.toggle()
return
}
DispatchQueue.main.async {
self?.signedIn = true
}
}
}
}
struct SignInView: View {
@State var email = ""
@State var password = ""
@EnvironmentObject var viewModel: AppViewModel
var body: some View {
VStack {
if viewModel.alert {
//SHOW ERROR
ErrorView(alert: viewModel.$alert, error: viewModel.$error)
Text("errors")
}
//SHOW SIGN IN FORM
//THERE IS CODE HERE TO CALL THE AUTH FUNCTIONS VIA BUTTONS ETC BUT APPARENTLY I'M NOT ALLOWED TO PUT THE WHOLE FILE IN THIS QUESTION
}
}
}
If I used published variables I get an error Failed to produce diagnostic for expression; please file a bug report at var body: some View. If I use state variables then the ErrorView is never shown.
ErrorView, we cannot solve the problem. Did you mean$viewModel.alertinstead ofviewModel.$alert?Swiftfile for each importantView