I am working on an app using coreData and a log in. For that, I have a LoginView and LoginViewModel that I use to get the info to the ContentView. I keep getting this error message: "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[abcText2Speech.UserEntity firstname]: unrecognized selector sent to instance 0x6000017f14c0'"
Here is my code where I think the issue lies :
this is from LoginViewModel:
func signup()-\> (UserEntity, Int){ @StateObject var dataController = DataController.shared buttonState = .loading(title: "Loading", systemImage: "person") DispatchQueue.main.asyncAfter(deadline: .now()+1.5){ if self.isValidEmail() && self.isValidPassword() && self.isValidName(){ self.isLoggedIn = true dataController.addUser(firstname: self.fname, lastname: self.lname, email: self.email, password: self.password) print("signed in") print(dataController.savedEntities[(dataController.savedEntities.count)-1].email ?? "email") print(self.isLoggedIn) self.buttonState = .enabled(title: "Login", systemImage: "checkmark.circle") } else{ self.buttonState = .disabled(title: "Fill out all fields to log in", systemImage: "exclamationmark.circle") } } print("Test") print(dataController.savedEntities[(dataController.savedEntities.count)-1].email ?? "email") return (dataController.savedEntities[(dataController.savedEntities.count)-1], 1) }
and this all prints with no issue.
this is the relevant part of sign up view
ActionButton(state: $model.buttonState, onTap:{ signup = model.signup() user = signup.0 }, backgroundColor: .primary , foregroundColor: Color(UIColor.systemBackground) ) NavigationLink (destination: LoginView()){ Text("Already have a profile? Click here to Log in") } } } label: { Label("Welcome!", systemImage: "hand.wave") } .padding() .textFieldStyle(.plain) let keyboard = abcTextViewModel() NavigationLink (destination: ContentView(viewModel: keyboard, currentUser: user) , isActive: $model.isLoggedIn){ EmptyView() }`
and this is the part of Content view that breaks it
Text(currentUser.firstname ?? "name")
these are the details of UserEntity
and this is the DataController code
import Foundation import CoreData class DataController: ObservableObject{ static let shared = DataController() let container: NSPersistentContainer @Published var savedEntities: [UserEntity] = [] init(){ container = NSPersistentContainer(name: "Model") container.loadPersistentStores { description, error in if let error = error { print("Core Data failed to load: \(error.localizedDescription)") } } fetchUsers() } func fetchUsers() { let request = NSFetchRequest<UserEntity>(entityName: "UserEntity") do { savedEntities = try container.viewContext.fetch(request) } catch let error { print("Error while fetching: \(error)") } } func addUser(firstname: String, lastname: String, email: String, password: String) { let newUser = UserEntity(context: container.viewContext) newUser.firstname = firstname newUser.lastname = lastname newUser.email = email newUser.id = password saveData() } func saveData(){ do { try container.viewContext.save() fetchUsers() } catch let error { print("Error saving: \(error)") } } }
any help would be greatly appreciated, I have been stuck on this one error for quite a while
This is where the code sends the error (on the struct abcText2SpeechApp line):
import SwiftUI @main struct abcText2SpeechApp: App { var body: some Scene { WindowGroup{ SignUpView() .environment(\.managedObjectContext, DataController.shared.container.viewContext) } } }
I tried printing the user details in login view model which worked, however that did not carry over to content view.