Create an Array in Swift

In this Tutorial, you will create an array that represents your bucket list: the things you would like to do in the future. Begin by declaring your first array.

Listing 8.1  Creating an array

import Cocoa

var str = "Hello, playground"

var bucketList: Array<String>

Here, you create a new variable called bucketList that is of the Array type. As you have seen, the var keyword means that bucketList is a variable. Arrays declared with var are called mutable – another way of saying they can be changed. There are also immutable arrays, which we will discuss later in this tutorial.

The <String> part of the declaration tells the bucketList array what sort of instances it can accept. Arrays can hold instances of any type; bucketList will accept instances of String.

There is a shorthand syntax for declaring an array. Make the following change in your playground:

Listing 8.2  Changing the syntax

var bucketList: Array<String>
var bucketList: [String]

This syntax does the same work, but it is more convenient. Here, the brackets identify bucketList as an instance of Array, and the String syntax tells bucketList what sort of values it can accept.

Your bucketList is declared, which means you have made a place to put an array – but you have not yet created the array itself. If you were to try to put an item in your bucketList, you would get an error saying that you are trying to add something before your bucketList is initialized.

Initialize the array with your first bucket list goal:

Listing 8.3  Initializing the array

var bucketList: [String]
bucketList = ["Climb Mt. Everest"]                         ["Climb Mt. Everest"]

You use the assignment operator = in conjunction with the Array literal syntax ["Climb Mt. Everest"]. An Array literal is a shorthand syntax that initializes an array with whatever instances you include. In this case, you initialize bucketList with an array containing a single item: "Climb Mt. Everest".

By the way, you are not required to initialize your variable with an array that contains elements. You can create an empty string with no characters: var empty = "". You can also create an empty array using [] to represent a literal array with no elements. You would then have an initialized array variable, ready for use.

As you have seen previously, you can declare and initialize on the same line. Update your declaration to also provide a value for your bucketList.

Listing 8.4  Initializing the array alongside its declaration

var bucketList: [String] = ["Climb Mt. Everest"]           ["Climb Mt. Everest"]
bucketList = ["Climb Mt. Everest"]

There is one more way you can simplify your declaration. As with other types, Array instances can be declared by taking advantage of Swift’s type inference capabilities. Remove the type declaration from your code to use type inference.

Listing 8.5  Using type inference

var bucketList: [String] = ["Climb Mt. Everest"]           ["Climb Mt. Everest"]

Your bucketList will still only accept instances of the String type, but now it infers the variable’s type based on the type of the instance used to initialize it. If you were to try to add an integer to this array, you would see an error telling you that you cannot add an instance of Int to your array, because it is expecting instances of the String type. In this way, your variable is of a single, compound type: an array of strings.

Note that if you are initializing an empty array using literal syntax, the compiler cannot infer the type of instance that the array contains. You need to declare it explicitly, as in var emptyStringArray: [String] = []. You can also use Swift’s constructor syntax, if you prefer: var emptyStringArray = [String]().

Now that you know how to create and initialize an array, it is time to learn how to access and modify your array’s elements.


Written by

XR Developer responsible for end-to-end development of XR solutions spanning multiple domains, by using various XR and WebXR libraries.

Leave a Reply