O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating and initializing arrays

We can initialize an array with an array literal. An array literal is a set of values that prepopulate the array. The following example shows how to define an immutable array of integers using the let keyword:

let arrayOne = [1,2,3] 

As mentioned, if we need to create a mutable array, we will use the var keyword to define the array. The following example shows how to define a mutable array:

var arrayTwo = [4,5,6] 

In the preceding two examples, the compiler inferred the type of values stored in the array by looking at the type of values stored in the array literal. If we want to create an empty array, we will need to explicitly declare the type of values to store in the array. There are two ways to declare ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required