Cannot use mutating member on immutable value: 'self' is immutable

why is error in swiftUI body?


Code Block swift
struct ContentView: View {
    @Converter("100", from: "USD", to: "CNY", rate: 6.78) var usd_cny
    var body: some View {
        VStack(alignment: .leading, spacing: 10) {
            Button(action: {
                update("200")
            }) {
                Text(self.$usd_cny)
            }
        }
    }
    mutating func update(_ price: String) {
        self.usd_cny = price
    }
}
@propertyWrapper
struct Converter {
    var from: String
    var to: String
    var rate: Double
    var value: Double
    var wrappedValue: String {
        get { "\(from)\(value)"}
        set { value = Double(newValue) ?? -1}
    }
    var projectedValue: String {
        return "\(to)\(value * rate)"
    }
    init(_ value: String, from: String, to: String, rate: Double) {
        self.rate = rate
        self.value = 0
        self.from = from
        self.to = to
        self.wrappedValue = value
    }
    mutating func update(_ value: String) {
        self.wrappedValue = value
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


var should be @ObservedObject to be modifiable.
@Claude31 which var
try to use @State var usd_cny and get rid of mutating in front of the update func
To use @State helped me .... Thank you

Just a little gist: (for others.. arriving here like me)

https://gist.github.com/ingconti/e0a2ceaf243ff2e27ed02144e506d996

Cannot use mutating member on immutable value: 'self' is immutable
 
 
Q