In an Xcode SwiftUI project, I have a TextField in which user write the username.
I want that the username will be kept in memory when the user close the app.
So, I created a UserData.swift in which there are utilisateur and a boolean var :
final class UserData: ObservableObject {
@Published var utilisateur = ""
@Published var connected = false
}
On my ContentView, I wrote this:
@EnvironmentObject var userData: UserData
and in the code, for each case I wrote one of these 3 possibilities.
self.userData.utilisateur
userData.utilisateur
userData.$utilisateur
Moreover, in SceneDelegate.swift, I wrote this :
window.rootViewController = UIHostingController(rootView: contentView.environmentObject(UserData()))
The problem is that when I close the app, the utilisateur value is deleted and not kept in memory.
Does someone know where is my mistake and what should I do to correct it?