A “mutating getter” in Swift is a property whose get
block has the mutating
modifier. For example, if your ProfileField
looked like this:
struct ProfileField {
var accessCount: Int = 0
var x: Int {
mutating get { // ← mutating getter here
accessCount++
return x
}
}
}
…then this code would generate your “Cannot use mutating getter on immutable value” error:
for field in _fields {
print(field.x)
}
Even though it doesn’t look like field.x
modifies field
, it does: it increments accessCount
. That’s why you must say var field
to make field mutable. (For loop iterators are let
by default.)
Without seeing either your ProfileField
or the body of your for loop, it’s impossible to say exactly why this occurs in your case. If you are not using mutating get
in ProfileField
itself, it may be happening in a struct nested within ProfileField
.