I have a struct
:
public struct MyStruct {
public var myInt: Int = 0
...
}
I have a extension of MyStruct
:
extension MyStruct {
public func updateValue(newValue: Int) {
// ERROR: Cannot assigned to property: 'self' is immutable
self.MyInt = newValue
}
}
I got the error showing above, I know I can fix the error by several ways, e.g. add a mutating
keyword before func
.
I am here not asking how to fix the error, but ask why swift doesn't allow this kind of value assignment ? I need an explanation besides a fix.