sak sak - 29 days ago 15
Swift Question

Swift 3 array var no longer mutable

I'm migrating a project from Swift 2.2 to 3.0, and I'm getting an error when concatenating arrays. Here's what used to work:

var x : [(index:Int, value:Float)] = []

for i in 0..<n {
x += [(i, Float(i))]


Left side of mutating operator isn't mutable: 'x' is immutable

In this case it is not clear to me why an array declared as a
would ever be immutble.

I have searched the Swift 3 documentation, and it is not clear to me which rules have changed which would cause these errors, or how to know when an array var will be immutable vs mutable.


It seems it's the array-type that makes a mess of things, you need:

var x: [(index:Int, value:Float)] = []

for i in 0..<n {
    x += [(index: i, value: Float(i))]

And those array-elements really look like they should be a struct of some type...