Search code examples
arraysswiftiteratorimmutabilityfor-in-loop

Mutability of the Iterator Element in a For-In loop with an Array in Swift


I have some code in Swift 3.0 like so for trying to update the property in a array of elements...

for point in listOfPoints {
    var pointInFrame : Float = Float(point.position.x * sensorIncomingViewPortSize.width) + Float(point.position.y)
    point.status = getUpdateStatus( pointInFrame )
}

However I get a compile error of: 'Cannot assign to property: 'point' is a 'let' constant' [for line 3]

Is there anyway to make the iterator (point) mutable in Swift, like how you can use 'inout' for a function parameter?

Or are you supposed to do this task another way?

Thanks in Advance.

Stan


Solution

  • Just change it to var instead of let where you declare point. let is a constant.