Search code examples
arraysswiftxcodevarlet

Difference between var array and let array?


I'm studying arrays in Swift and in my book first they write:

let numbers = [0, 1, 2, 3]

but then write:

var numbers = [0, 1, 2, 3]

I know that let denotes constants and var refers to variables, but practically what changes from an array declared as constant and an array declared as variable?


Solution

  • since arrays in swift are structs declaring an array with let not only prevents you from assigning a new value to it but also prevents you from changing its contents

    so for example:

        let arr = [0, 1, 2]
        arr[0] = 10 //will not compile
        arr = [] //will not compile