Structs aren’t immutable. However, what they are something called value types which means they have implicit copying when passed to functions. If you pass a struct to a function and perform an action on the struct inside the function that mutates it, the outer version of the struct remains unchanged since the one passed to the function was in fact a copy.
Consider the following:
struct Foo {
var value: Int
}
func bar(_ f: Foo) {
f.value += 1
}
var f = Foo(value: 1)
bar(f)
// f.value is still 1
In order to mutate a struct inside a function you must explicitly pass it as inout:
func baz(_ f: inout Foo) {
f.value += 1
}
baz(&f)
// f.value is now 2
Edit: note that I haven’t used Swift UI and I don’t really know how the behaviour of structs applies to that particular framework. This answer is about structs in Swift in the general sense.




