Search code examples
iosswiftuisettingsuser-datauserdefaults

Save String from more than 1 TextField in UserDefaults with SwiftUI


I can't figure out how to save the userData for more than 1 Textfield.

I managed that the data gets saved for 1 Textfield but if i "duplicate" the code and the Textfield, it only saves one textfield userData...

My userData file:

import SwiftUI
import Combine

class UserData : ObservableObject {

 private static let userDefaultBuyingPrice = "BuyingPrice"
 private static let userDefaultRent = "Rent"


 @Published var BuyingPrice = UserDefaults.standard.string(forKey: UserData.userDefaultBuyingPrice) ?? ""

  @Published var Rent = UserDefaults.standard.string(forKey: UserData.userDefaultRent) ?? ""
  private var canc: AnyCancellable!

 }

My ContentView File:

struct ContentView: View {

@ObservedObject var userData = UserData()

var body: some View {
    VStack{
        TextField("BuyingPrice", text: $userData.BuyingPrice)
            .font(.title)
            .keyboardType(.decimalPad)

        TextField("Rent", text: $userData.Rent)
            .font(.title)
            .keyboardType(.decimalPad)
    }
}

}

Only the second value is saved, cannot figure out why the second one is not working

If there is a easier solution for the whole userData saving i would be grateful for the input.

THanks,


Solution

  • UserData:

    import SwiftUI
    import Combine
    
    class UserData : ObservableObject {
    
      private static let userDefaultBuyingPrice = "BuyingPrice"
      private static let userDefaultRent = "Rent"
    
    
      @Published var BuyingPrice = UserDefaults.standard.string(forKey: UserData.userDefaultBuyingPrice) ?? "" {
        didSet {
          UserDefaults.standard.set(self.BuyingPrice, forKey: UserData.userDefaultBuyingPrice)
        }
      }
    
      @Published var Rent = UserDefaults.standard.string(forKey: UserData.userDefaultRent) ?? "" {
        didSet {
          UserDefaults.standard.set(self.Rent, forKey: UserData.userDefaultRent)
        }
      }
      private var canc: AnyCancellable!
    
    }
    

    ContentView:

    import SwiftUI
    
    struct ContentView: View {
      @EnvironmentObject var userData: UserData
    
       var body: some View {
           VStack{
               TextField("BuyingPrice", text: $userData.BuyingPrice)
                   .font(.title)
                   .keyboardType(.decimalPad)
    
               TextField("Rent", text: $userData.Rent)
                   .font(.title)
                   .keyboardType(.decimalPad)
           }
       }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
          ContentView().environmentObject(UserData())
        }
    }
    

    In SceneDelegate.swift:

    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
      var window: UIWindow?
      var userData = UserData() //add this line
    
    //then modify this  line:
    window.rootViewController = UIHostingController(rootView: contentView)
    //to this:
    window.rootViewController = UIHostingController(rootView: contentView.environmentObject(userData))