Arrays
An array is an ordered collection of elements of the same type, and they are used for pretty much anything that requires storing things in a certain order, such as the contents of lists in apps. It works like similar types in other languages.
Working with Arrays
Follow these steps to work with arrays:
- We can create an array like this:
let a = [0,1,2,3,4] // array literal
- We can join two arrays like this:
var b = a + [5,6] // join two arrays
- We can have a repeated value like this:
let c = Array(repeating: 4.1, count: 3) // repeat one value
- To create an array from any sequence, we can do this:
// create from any Sequence (a String is a Sequence of Character) var d = Array("The ☀ and 🌙 ")
- To append a value to an array, use this:
b.append(10) // append ...
Get Beginning Swift now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.