Swift - Mutability of Arrays


An array is an indexed collection of objects.

The following statement shows an array containing three items:

var nameArray = ["iOS", "Android", "Database"]

In Swift, you create an array using the [] syntax.

The compiler automatically infers the type of items inside the array.

Here, it is an array of String elements.

In Swift, arrays are implemented internally as structures, not as classes.

When you declare an array using the var keyword, it is a mutable array.

The array's size is not fixed and during run time you can add or remove elements from the array.

If you declare the array using the let keyword, you are creating an immutable array, which means that once the array is created, its element(s) cannot be removed, nor can new elements be added:

//immutable array
let nameArray = ["iOS", "Android", "Windows"]

Related Topic