0

let type variable are supposed to be constants, but reassign a value later does not throw any error.

How do I create a non-mutable array.

  let testArray = ["Catfish","water","tulips"]
  print(testArray[1])
  testArray[1] = "bottle of water"
  print(testArray[1])

The output is "waterbottle of water"

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
NNikN
  • 3,720
  • 6
  • 44
  • 86
  • possible duplicate of [How do you create an immutable array in Swift?](http://stackoverflow.com/questions/24090741/how-do-you-create-an-immutable-array-in-swift) – Matt Gibson Jun 30 '14 at 06:12

1 Answers1

1

You aren't reassinging the array itself, but an element of the array. This is allowed in Swift. There isn't currently a way to make all of the elements in the array immutable. It should be changed in a future update though.

You may also want to take a look at this. Arrays have interesting assignment and copying behavior in Swift.

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142