0

I have a simple array of structs

var fields: [ProfileField]?

when I try to iterate through this array, swift shows me error on this code:

guard let _fields = fields else {return}
for field in _fields {
}

the error is:

Cannot use mutating getter on immutable value: 'field' is a 'let' constant

And this code compiles well:

for var field in _fields {
}

Why do I need to declare field as var?

Paul Cantrell
  • 9,175
  • 2
  • 40
  • 48
ShurupuS
  • 2,923
  • 2
  • 24
  • 44

1 Answers1

2

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.

Paul Cantrell
  • 9,175
  • 2
  • 40
  • 48
  • Great! Thank you so much for the "Even though" - now I understand that accessCount is changing! Best of the best! – ShurupuS Oct 31 '15 at 03:55
  • 1
    Glad it helped. Further surprising detail (that makes sense when you think about it): it’s not `accessCount++` that causes the compile error, but the word `mutating` itself. If a function modifies the struct, you have to declare it `mutating`, but the converse is not true. You can declare something `mutating` just to reserve the right to modify the struct even though you don’t. It’s an API choice. – Paul Cantrell Oct 31 '15 at 16:10