Structs are immutable meaning that they can't change. I must clearly misunderstand this whole concept as it seems that sometimes I do get to change the struct, and othertimes I don't.
Consider this code:
struct SomeStruct: View {
var someProperty = ""
var someComputedProperty: String {
get { someProperty }
set { someProperty = "a" } // WORKD
}
func someFunc() {
someProperty = "b" // ERROR; BUT WORKS WITH mutating func
}
var body: some View {
Button("SomeButton", action: {
someProperty = "c" // ERROR
})
}
}
struct SomeOtherStruct {
func someOtherFunct() {
var someStruct = SomeStruct()
someStruct.someProperty = "THIS WORKS"
}
}
The only place where it's not actually allowed to change the struct is in the Button
closure
. Why is it sometimes allowed, and othertimes not? Or have I simply missunderstood what immutability actually implies?