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"

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.