.append()
The .append()
method will add an element to the end of an array. It allows for single and multiple element additions.
Syntax
arrayName.append(element)
arrayName.append(contentsOf: [element1, element2, ...])
The method is called on an array instance, and one or more elements can be added by using the contentsOf:
parameter.
Example
var topBabyNames = ["Sophia", "Liam"]topBabyNames.append("Riley")print(topBabyNames)topBabyNames.append(contentsOf: ["Olivia", "Noah"])print(topBabyNames)
This will output:
["Sophia", "Liam", "Riley"]["Sophia", "Liam", "Riley", "Olivia", "Noah"]
Note: Big-O of this method is O(1).
All contributors
- grace_k29 total contributions
- Christine_Yang271 total contributions
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.