1

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,

1 Answers1

0

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))
Monica Granbois
  • 6,632
  • 1
  • 17
  • 17
  • Had the problem for a few days now and finally it is solved. Also I have forgotten the whole "SceneDelegate Part". Thank you very much. –  Feb 19 '20 at 17:38