struct Test{
var param1 = false
func method1() {
param1 = true
}
}
param1 = true
will throw the exception Cannot assign to property: 'self' is immutable
The reason is that struct
is saved in stack memory, so its parameters cannot be modified.
But we can change it through mutating
:
struct Test{
var param1 = false
mutating func method1() {
param1 = true
}
}
Refer to stack overflow