Start learning free

LESSON 94 OF 415 · ARRAYS I

Arrays in Swift: Creating and Initializing Them

Lesson Read the full explanation

A big limitation of static arrays in Swift is the fact that once the array is created, it has a fixed size.

The Array declared with var overcomes this limitation by having variable size and allowing for elements to be added or removed.


Creation:

We create an empty Array by specifying the type of the elements inside [] followed by parenthesis:

[String]()

This code creates an empty Array where each item is a String.


Type Annotation:

A good practice is to annotate the type of the created Array by specifying the element type in the variable declaration, like this:

var sandwich: [String] = []

Initialization:

You can also initialize an Array at creation by using an array literal and put a comma between each entry, like this:

var sandwich: [String] = [
    "peanut butter", "jelly", "bread"
]

We created an Array named sandwich which consists of 3 items: "peanut butter", "jelly" and "bread".


Assignment
Follow the Coding Tutorial and let's create some Arrays!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    SWIFT · ARRAYS I

    Arrays in Swift, explained

    From fixed to flexible

    The previous lesson created arrays whose size never changed. A Swift array declared with var can do more: it can grow and shrink while the program runs, with append to add an element and remove(at:) to take one out. The type is the same Array, written [Element]. What makes the difference is the keyword you declare it with.

    var sandwich: [String] = ["bread"]
    sandwich.append("jelly")
    print(sandwich)        // ["bread", "jelly"]
    sandwich.remove(at: 0)
    print(sandwich)        // ["jelly"]
    

    If sandwich were declared with let, both calls would be compile errors, because a let array can't change in any way.

    Ways to create an array

    An empty array

    To start with nothing, name the element type and add empty parentheses:

    var empty = [String]()
    print(empty.count)  // 0
    

    The compiler needs the element type, because an empty pair of brackets gives it nothing to infer from. That is why [String] appears here.

    A type annotation

    You can state the type on the variable instead, and then assign an empty literal:

    var sandwich: [String] = []
    

    The annotation after the colon says what the variable holds. Writing var sandwich = [] without it fails to compile, since the element type can't be guessed. Annotations are also useful when you want a different type from the one Swift would infer, for example let weights: [Double] = [1, 2, 3] stores doubles, while [1, 2, 3] alone would infer [Int].

    An array literal

    If you already know the items, list them between brackets, separated by commas:

    var sandwich: [String] = [
        "peanut butter", "jelly", "bread"
    ]
    print(sandwich.count)  // 3
    

    A literal can span several lines, and Swift also accepts a trailing comma after the last item. With the values in place, the annotation is optional: var sandwich = ["peanut butter", "jelly", "bread"] is inferred as [String].

    Arrays are values

    Assigning an array to another variable copies it, so later changes to one don't affect the other:

    var original = [14, 32, 50]
    var backup = original
    original[0] = 99
    print(original)  // [99, 32, 50]
    print(backup)    // [14, 32, 50]
    

    This differs from languages where two names share one array.

    The exercise, step by step

    1. Create fruits. Declare it with let and an array literal holding "mango", "kiwi" and "grapefruit". Swift infers [String] from the values, so no annotation is needed.
    2. Create ages. Declare it with let and the literal [14, 32, 50, 21]. These are whole numbers, so the type is [Int].
    let fruits = ["mango", "kiwi", "grapefruit"]
    let ages = [14, 32, 50, 21]
    

    Both are constants. If a later task needs to add to them, change let to var at that point.

    Common mistakes

    • Using let and then appending. let arrays reject append, remove(at:) and element assignment. The error message says the array is a let constant.
    • Using var for everything. The code works, but let tells readers, and the compiler, that the array never changes.
    • Forgetting the type for an empty array. var items = [] doesn't compile. Write [String]() or var items: [String] = [].
    • Mixing types in a literal. [14, "thirty"] is rejected with a message about a heterogeneous collection literal, unless you annotate the array as [Any], which gives up the type checking. Keep each array to one element type.

    Key takeaways

    • A var array can change size with append and remove(at:); a let array can't change.
    • Create an empty array with [Type]() or with an annotation and [].
    • An array literal lists the items and lets Swift infer the type.
    • Assigning an array copies it.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free