0

Why does the following code work? Trying to seek clarity around how Swift treats consts.

let list = [1, 2, 3, 4, 5]
list[1] = 0

EDIT: This is a dup of Is there a reason that Swift array assignment is inconsistent (neither a reference nor a deep copy)?

I need to get better at searching :)

Community
  • 1
  • 1
andybons
  • 382
  • 1
  • 2
  • 10

2 Answers2

1

An array being const in Swift is a bit unlike other things in Swift. When an array is const, it just means that it's length can't be altered, individual elements can still be changed.

Alex Gaynor
  • 14,353
  • 9
  • 63
  • 113
1

To use a C equivalent, list is like a 'constant pointer to a non-constant object'. That means that you cannot change the array that list points to, but you can change its contents.

Paul Manta
  • 30,618
  • 31
  • 128
  • 208