Start learning free

LESSON 95 OF 415 · ARRAYS I

Accessing Static Array Elements in Swift

Lesson Read the full explanation

Arrays are ordered, meaning each element has a numbered position known as its index. We access an array element by referring to its index number.

Arrays use zero-based indexing, so the first element in an array has an index of 0, the second element has an index of 1, etc.

We're using bracket notation ([]) with the index after the name of the array to access the element:

let animals = ["cat", "dog", "parrot"]

print(animals[0]) // Output: cat
print(animals[1]) // Output: dog
print(animals[2]) // Output: parrot

Assignment
Follow the Coding Tutorial and let's practice with accessing elements!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 3
    Ctrl + Enter to check

    SWIFT · ARRAYS I

    Accessing Static Array Elements in Swift, explained

    Indexes and the subscript

    Every element of an array has a position called its index. Swift arrays use zero-based indexing: the first element is at index 0, the second at index 1, and so on. To read an element, write the array's name followed by the index in square brackets. This is the array's subscript.

    let animals = ["cat", "dog", "parrot"]
    
    print(animals[0])  // cat
    print(animals[1])  // dog
    print(animals[2])  // parrot
    

    An element read this way is an ordinary value of the array's element type. animals[1] is a String, so you can store it in a constant or pass it to a function:

    let pet = animals[1]
    print(pet)  // dog
    

    Using elements in expressions

    For an array of Int, each element is an Int, so arithmetic works on elements directly. The whole expression inside print is evaluated first, and print receives the result.

    let ages = [14, 32, 50, 21]
    print(ages[0] + ages[1])  // 46
    

    The last element

    An array with four elements has indexes 0, 1, 2 and 3. The last index is always one less than the number of elements, which you get from count. Both of these refer to the last element of ages:

    print(ages[3])
    print(ages[ages.count - 1])  // 21
    

    Swift also offers ages.last, but it returns an optional, because an empty array has no last element. Indexing with count - 1 gives a plain Int, which is what these exercises use. Swift does not accept a negative index such as ages[-1] to count from the end.

    The exercise, step by step

    The starter code declares two constant arrays, and each step adds one print.

    1. Print the second fruit. The second item has index 1, not 2, so the statement is print(fruits[1]). It prints kiwi.
    2. Print the sum of the first three ages. These are the elements at indexes 0, 1 and 2. Add them inside the call: print(ages[0] + ages[1] + ages[2]). That prints 96, since 14 + 32 + 50 = 96. The element at index 3 is not part of the sum.
    3. Print the difference between the last and the first age. The array holds four numbers, so the last one is at index 3. The statement is print(ages[3] - ages[0]), which prints 7 (21 - 14). Keep the order: last minus first. Reversing it gives -7.
    let fruits = ["mango", "kiwi", "grapefruit"]
    let ages = [14, 32, 50, 21]
    
    print(fruits[1])
    print(ages[0] + ages[1] + ages[2])
    print(ages[3] - ages[0])
    

    The output is three lines: kiwi, 96 and 7.

    Common mistakes

    • Counting from 1. fruits[2] is the third fruit, grapefruit, not the second.
    • Reading past the end. With four ages, ages[4] is invalid. The program crashes at run time. The next lessons look at this error more closely.
    • Using a non-integer index. The index must be an Int. A String or a Double is a compile error.
    • Joining numbers as text. print(ages[0], ages[1]) prints the two numbers separated by a space. It does not add them. Use + between the elements.

    Key takeaways

    • Indexes start at 0, and the last index is count - 1.
    • Read an element with array[index]; the result has the array's element type.
    • Elements can be used in arithmetic and in any other expression.
    • An index outside the array crashes the program.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free