Start learning free

LESSON 103 OF 415 · ARRAYS I

Array Push in Swift: Appending with append

Lesson Read the full explanation

We can add elements to the end of an Array using the .append() method.

.append() takes one parameter and "pushes" it onto the end of the Array:

var arr1 = [1, 2, 3]
arr1.append(4)

// arr1 is now [1, 2, 3, 4]

Assignment
Follow the Coding Tutorial and let's play with some arrays.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    SWIFT · ARRAYS I

    Array Push in Swift, explained

    What append does

    Swift has no method called push. To add an element to the end of an array, you call append. It takes one value, puts it after the last element and makes the array one element longer.

    var arr1 = [1, 2, 3]
    arr1.append(4)
    print(arr1)
    
    [1, 2, 3, 4]
    

    The value you append must match the array's element type. An array of String accepts only strings, and an array of Int accepts only integers. Swift infers that type from the literal the array starts with, so ["mango", "kiwi"] is a [String].

    The array must be a var

    append changes the array it is called on. Swift calls such a method mutating, and it is only allowed on a variable. If the array was declared with let, the compiler refuses:

    let fruits = ["mango", "kiwi"]
    fruits.append("apple")
    
    error: cannot use mutating member on immutable value: 'fruits' is a 'let' constant
    

    The fix is to declare the array with var. Use let for arrays that never change and var for arrays that grow or shrink.

    Adding more than one element

    append takes a single value per call, so adding two values takes two calls. When the values already sit in another array, append(contentsOf:) adds them all at once:

    var ages = [14, 32, 50, 21]
    ages.append(10)
    ages.append(23)
    print(ages)
    
    var more = [14, 32]
    more.append(contentsOf: [10, 23])
    print(more)
    
    [14, 32, 50, 21, 10, 23]
    [14, 32, 10, 23]
    

    If you need a value somewhere other than the end, insert(_:at:) puts it at a given index and shifts the later elements along: ages.insert(99, at: 1) turns [14, 32, 50, 21] into [14, 99, 32, 50, 21].

    Other ways to add to the end

    The += operator appends the elements of another array to a var array. The + operator builds a new array and leaves both operands alone:

    var nums = [1, 2]
    nums += [3]
    print(nums)
    let more = nums + [4, 5]
    print(more, nums)
    
    [1, 2, 3]
    [1, 2, 3, 4, 5] [1, 2, 3]
    

    Reach for append when you add a single value, because it says exactly what happens.

    Starting from an empty array

    Appending is how most arrays get filled. Start with an empty array of a stated type, then add elements as they arrive. count tells you how many there are, and last is an optional holding the final element:

    var names: [String] = []
    print(names.count, names.isEmpty)
    names.append("Ann")
    names.append("Bo")
    print(names.count, names.last!)
    
    0 true
    2 Bo
    

    The type annotation [String] is needed here because an empty literal [] gives Swift nothing to infer the element type from.

    Arrays are copied, not shared

    An array is a value type. Assigning it to another variable makes an independent copy, so appending to one does not change the other:

    var a = [1, 2, 3]
    var b = a
    b.append(4)
    print(a, b)
    
    [1, 2, 3] [1, 2, 3, 4]
    

    The exercise

    The starter code has two variable arrays. The lesson asks for two changes:

    1. Call append on fruits with "apple". The array had three fruits, so "apple" becomes the fourth and last.
    2. Call append on ages twice, first with 10 and then with 23. Because each call adds to the end, the order of the calls is the order in the array, and 23 ends up last.
    var fruits = ["mango", "kiwi", "grapefruit"]
    var ages = [14, 32, 50, 21]
    
    fruits.append("apple")
    ages.append(10)
    ages.append(23)
    

    Afterwards fruits is ["mango", "kiwi", "grapefruit", "apple"] and ages is [14, 32, 50, 21, 10, 23].

    Common mistakes

    • Using a let array. The compiler error above means the array needs var.
    • Looking for push. Other languages use that name. In Swift it does not exist on arrays.
    • Appending the wrong type. ages.append("10") is a compile error, because "10" is a string and ages holds integers.
    • Passing two values to one call. ages.append(10, 23) does not compile. Call append twice, or use append(contentsOf:) with an array.

    Key takeaways

    • append adds one element to the end of an array.
    • The array must be declared with var.
    • append(contentsOf:) adds a whole sequence, and insert(_:at:) adds at a chosen index.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free