Arrays and dictionaries in Swift are objects that contain collections of other objects. In this lesson, we will cover some of the basics of working with arrays and dictionaries in Swift.

Mutable and immutable collections

Collections in Swift come in mutable and immutable forms. The contents of immutable collection instances cannot be changed after the object has been initialized. To make a collection immutable, assign it to a constant when it is created using the let keyword. On the other hand, collections are mutable if assigned to a variable (using var).

Swift array initialization

An array is a data type designed specifically to hold multiple values in a single ordered collection. An array, for example, could be created to store a list of String values. Strictly speaking, a single Swift-based array is only able to store values that are of the same type. An array declared as containing String values, therefore, could not also contain an Int value. However, it is also possible to create mixed type arrays. This will BE demonstrated later in this chapter. The type of an array can be specified using type annotation or left to the compiler to identify using type inference.

An array may be initialized with a collection of values (referred to as an array literal) at creation time using the following syntax:

var variableName: [type] = [value 1, value2, value3, ……. ]

The following code creates a new array assigned to a variable (thereby making it mutable) that is initialized with three string values:

var treeArray = ["Pine", "Oak", "Yew"]

Alternatively, the same array could have been created immutably by assigning it to a constant:

let treeArray = ["Pine", "Oak", "Yew"]

In the above instances, the Swift compiler will use type inference to decide that the array contains values of String type and prevents values of other types from being inserted into the array elsewhere within the application code.

Alternatively, the same array could have been declared using type annotation:

var treeArray: [String] = ["Pine", "Oak", "Yew"]

Arrays do not have to have values assigned at creation time. The following syntax can be used to create an empty array:

var variableName = [type]()

Consider, for example, the following code which creates an empty array designated to store floating-point values and assigns it to a variable named priceArray:

var priceArray = [Float]()

Another useful initialization technique allows an array to be initialized to a certain size with each array element pre-set with a specified default value:

var nameArray = [String](repeating: "My String", count: 10) 

When compiled and executed, the above code will create a new 10 element array with each element initialized with a string that reads “My String.”

Finally, a new array may be created by adding together two existing arrays (assuming both arrays contain values of the same type). For example:

let firstArray = ["Red", "Green", "Blue"]
let secondArray = ["Indigo", "Violet"]
 
let thirdArray = firstArray + secondArray

Create a free account to view this lesson.

Continue your learning journey with a 14-day free trial.

By signing up, you agree to Educative's Terms of Service and Privacy Policy