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,