Array(opens new window) is an ordered collection type in the Swift standard library. It provides O(1) random access and dynamic reallocation. Array is a generic type(opens new window), so the type of values it contains are known at compile time.
As Array is a value type(opens new window), its mutability is defined by whether it is annotated as a var (mutable) or let (immutable).
// A mutable array of Strings, initially empty.var arrayOfStrings:[String]=[]// type annotation + array literalvar arrayOfStrings =[String]()// invoking the [String] initializervar arrayOfStrings =Array<String>()// without syntactic sugar
Array literals
An array literal is written with square brackets surrounding comma-separated elements:
// Create an immutable array of type [Int] containing 2, 4, and 7let arrayOfInts =[2,4,7]
The compiler can usually infer the type of an array based on the elements in the literal, but explicit type annotations can override the default:
let arrayOfUInt8s:[UInt8]=[2,4,7]// type annotation on the variablelet arrayOfUInt8s =[2,4,7]as[UInt8]// type annotation on the initializer expressionlet arrayOfUInt8s =[2asUInt8,4,7]// explicit for one element, inferred for the others
Arrays with repeated values
// An immutable array of type [String], containing ["Example", "Example", "Example"]let arrayOfStrings =Array(repeating:"Example",count:3)
Creating arrays from other sequences
let dictionary =["foo":4,"bar":6]// An immutable array of type [(String, Int)], containing [("bar", 6), ("foo", 4)]let arrayOfKeyValuePairs =Array(dictionary)
Multi-dimensional arrays
In Swift, a multidimensional array is created by nesting arrays: a 2-dimensional array of Int is [[Int]] (or Array<Array<Int>>).
let array2x3 =[[1,2,3],[4,5,6]]// array2x3[0][1] is 2, and array2x3[1][2] is 6.
To create a multidimensional array of repeated values, use nested calls of the array initializer:
var array3x4x5 =Array(repeating:Array(repeating:Array(repeating:0,count:5),count:4),count:3)
Extracting values of a given type from an Array with flatMap(_????
The things Array contains values of Any type.
let things:[Any]=[1,"Hello",2,true,false,"World",3]
We can extract values of a given type and create a new Array of that specific type. Let's say we want to extract all the Int(s) and put them into an Int Array in a safe way.
let numbers = things.flatMap {$0as?Int}
Now numbers is defined as [Int]. The flatMap function discard all nil elements and the result thus contains only the following values:
[1,2,3]
Flattening the result of an Array transformation with flatMap(_????
Each sequence from the transformation will be concatenated, resulting in an array containing the combined elements of each sequence – [S.Generator.Element].
Combining the characters in an array of strings
For example, we can use it to take an array of prime strings and combine their characters into a single array:
The result is then an array containing the combined elements of all the sequences from each of the transformation closure calls – [String.CharacterView.Generator.Element].
Flattening a multidimensional array
As flatMap(_:) will concatenate the sequences returned from the transformation closure calls, it can be used to flatten a multidimensional array – such as a 2D array into a 1D array, a 3D array into a 2D array etc.
This can simply be done by returning the given element $0 (a nested array) in the closure:
// A 2D array of type [[Int]]let array2D =[[1,3],[4],[6,8,10],[11]]// A 1D array of type [Int]let flattenedArray = array2D.flatMap {$0}print(flattenedArray)// [1, 3, 4, 6, 8, 10, 11]
Combining an Array's elements with reduce(_:combine:)
reduce(_:combine:)(opens new window) can be used in order to combine the elements of a sequence into a single value. It takes an initial value for the result, as well as a closure(opens new window) to apply to each element – which will return the new accumulated value.
For example, we can use it to sum an array of numbers:
let numbers =[2,5,7,8,10,4]let sum = numbers.reduce(0){accumulator, element inreturn accumulator + element
}print(sum)// 36
We're passing 0 into the initial value, as that's the logical initial value for a summation. If we passed in a value of N, the resulting sum would be N + 36. The closure passed to reduce has two arguments. accumulator is the current accumulated value, which is assigned the value that the closure returns at each iteration. element is the current element in the iteration.
As in this example, we're passing an (Int, Int) -> Int closure to reduce, which is simply outputting the addition of the two inputs – we can actually pass in the + operator directly, as operators are functions in Swift:
let sum = numbers.reduce(0, combine:+)
Filtering out nil from an Array transformation with flatMap(_????
The difference with this version of flatMap(_:) is that it expects the transform closure(opens new window) to return an Optional(opens new window) value T? for each of the elements. It will then safely unwrap each of these optional values, filtering out nil – resulting in an array of [T].
For example, lazy flattening a 2D array into a 1D array:
// A 2D array of type [[Int]]let array2D =[[1,3],[4],[6,8,10],[11]]// A FlattenBidirectionalCollection<[[Int]]>let lazilyFlattenedArray = array2D.flatten()print(lazilyFlattenedArray.contains(4))// true
In the above example, flatten() will return a FlattenBidirectionalCollection(opens new window), which will lazily apply the flattening of the array. Therefore contains(_:)(opens new window) will only require the first two nested arrays of array2D to be flattened – as it will short-circuit upon finding the desired element.
Subscripting an Array with a Range
One can extract a series of consecutive elements from an Array using a Range.
let words =["Hey","Hello","Bonjour","Welcome","Hi","Hola"]let range =2...4let slice = words[range]// ["Bonjour", "Welcome", "Hi"]
Subscripting an Array with a Range returns an ArraySlice. It's a subsequence of the Array.
In our example, we have an Array of Strings, so we get back ArraySlice<String>.
Although an ArraySlice conforms to CollectionType and can be used with sort, filter, etc, its purpose is not for long-term storage but for transient computations: it should be converted back into an Array as soon as you've finished working with it.
For this, use the Array() initializer:
let result =Array(slice)
To sum up in a simple example without intermediary steps:
let words =["Hey","Hello","Bonjour","Welcome","Hi","Hola"]let selectedWords =Array(words[2...4])// ["Bonjour", "Welcome", "Hi"]
Removing element from an array without knowing it's index
Generally, if we want to remove an element from an array, we need to know it's index so that we can remove it easily using remove(at:) function.
But what if we don't know the index but we know the value of element to be removed!
So here is the simple extension to an array which will allow us to remove an element from array easily without knowing it's index:
var array = ["abc", "lmn", "pqr", "stu", "xyz"]
array.remove("lmn")
print("\(array)") //["abc", "pqr", "stu", "xyz"]
array.remove("nonexistent")
print("\(array)") //["abc", "pqr", "stu", "xyz"]
//if provided element value is not present, then it will do nothing!
Also if, by mistake, we did something like this: array.remove(25)
i.e. we provided value with different data type, compiler will throw an error mentioning- cannot convert value to expected argument type
For example, filtering even numbers from an [Int]:
let numbers =[22,41,23,30]let evenNumbers = numbers.filter {$0%2==0}print(evenNumbers)// [22, 30]
Filtering a [Person], where their age is less than 30:
structPerson{var age :Int}let people =[Person(age:22),Person(age:41),Person(age:23),Person(age:30)]let peopleYoungerThan30 = people.filter {$0.age <30}print(peopleYoungerThan30)// [Person(age: 22), Person(age: 23)]
Sorting an Array of Strings
The most simple way is to use sorted():
let words =["Hello","Bonjour","Salute","Ahola"]let sortedWords = words.sorted()print(sortedWords)// ["Ahola", "Bonjour", "Hello", "Salute"]
or sort()
var mutableWords =["Hello","Bonjour","Salute","Ahola"]
mutableWords.sort()print(mutableWords)// ["Ahola", "Bonjour", "Hello", "Salute"]
You can pass a closure as an argument for sorting:
let words =["Hello","Bonjour","Salute","Ahola"]let sortedWords = words.sorted(isOrderedBefore:{$0>$1})print(sortedWords)// ["Salute", "Hello", "Bonjour", "Ahola"]
Alternative syntax with a trailing closure:
let words =["Hello","Bonjour","Salute","Ahola"]let sortedWords = words.sorted(){$0>$1}print(sortedWords)// ["Salute", "Hello", "Bonjour", "Ahola"]
But there will be unexpected results if the elements in the array are not consistent:
let words =["Hello","bonjour","Salute","ahola"]let unexpected = words.sorted()print(unexpected)// ["Hello", "Salute", "ahola", "bonjour"]
To address this issue, either sort on a lowercase version of the elements:
let words =["Hello","bonjour","Salute","ahola"]let sortedWords = words.sorted {$0.lowercased()<$1.lowercased()}print(sortedWords)// ["ahola", "bonjour", "Hello", "Salute"]
Or import Foundation and use NSString's comparison methods like caseInsensitiveCompare:
let words =["Hello","bonjour","Salute","ahola"]let sortedWords = words.sorted {$0.caseInsensitiveCompare($1)==.orderedAscending }print(sortedWords)// ["ahola", "bonjour", "Hello", "Salute"]
Alternatively, use localizedCaseInsensitiveCompare, which can manage diacritics.
To properly sort Strings by the numeric value they contain, use compare with the .numeric option:
Note: In order to use the above methods, the elements must conform to the Comparable(opens new window) protocol.
Sorting an array with a custom ordering
You may also sort an array using a closure(opens new window) to define whether one element should be ordered before another – which isn't restricted to arrays where the elements must be Comparable. For example, it doesn't make sense for a Landmark to be Comparable – but you can still sort an array of landmarks by height or name.
structLandmark{let name :Stringlet metersTall :Int}var landmarks =[Landmark(name:"Empire State Building", metersTall:443),Landmark(name:"Eifell Tower", metersTall:300),Landmark(name:"The Shard", metersTall:310)]
// sort landmarks by height (ascending)
landmarks.sortInPlace {$0.metersTall <$1.metersTall}print(landmarks)// [Landmark(name: "Eifell Tower", metersTall: 300), Landmark(name: "The Shard", metersTall: 310), Landmark(name: "Empire State Building", metersTall: 443)]// create new array of landmarks sorted by namelet alphabeticalLandmarks = landmarks.sort {$0.name <$1.name}print(alphabeticalLandmarks)// [Landmark(name: "Eifell Tower", metersTall: 300), Landmark(name: "Empire State Building", metersTall: 443), Landmark(name: "The Shard", metersTall: 310)]
// sort landmarks by height (ascending)
landmarks.sort {$0.metersTall <$1.metersTall}// create new array of landmarks sorted by namelet alphabeticalLandmarks = landmarks.sorted {$0.name <$1.name}
let numbers =[1,2,3,4,5]let words = numbers.map {String($0)}print(words)// ["1", "2", "3", "4", "5"]
map(_:) will iterate through the array, applying the given closure to each element. The result of that closure will be used to populate a new array with the transformed elements.
Since String has an initialiser that receives an Int we can also use this clearer syntax:
let words = numbers.map(String.init)
A map(_:) transform need not change the type of the array – for example, it could also be used to multiply an array of Ints by two:
let minimumNumber = numbers.min()// Optional(1)let maximumNumber = numbers.max()// Optional(25)
The returned values from these methods are Optional(opens new window) to reflect the fact that the array could be empty – if it is, nil will be returned.
Finding the minimum or maximum element with a custom ordering
You may also use the above methods with a custom closure(opens new window), defining whether one element should be ordered before another, allowing you to find the minimum or maximum element in an array where the elements aren't necessarily Comparable.
Copied arrays will share the same space in memory as the original until they are changed. As a result of this there is a performance hit when the copied array is given its own space in memory as it is changed for the first time.
Accessing Array Values
The following examples will use this array to demonstrate accessing values
var exampleArray:[Int]=[1,2,3,4,5]//exampleArray = [1, 2, 3, 4, 5]
To access a value at a known index use the following syntax:
let exampleOne = exampleArray[2]//exampleOne = 3
Note: The value at index two is the third value in the Array. Arrays use a zero based index which means the first element in the Array is at index 0.
we can group the boxes by the thingsInside property in order to get a Dictionary where the key is the number of things and the value is an array of boxes.
let grouped = boxes.reduce([Int:[Box]]()){(res, box)->[Int:[Box]]invar res = res
res[box.thingsInside]=(res[box.thingsInside]??[])+[box]return res
}
Now grouped is a [Int:[Box]] and has the following content
The zip function accepts 2 parameters of type SequenceType and returns a Zip2Sequence where each element contains a value from the first sequence and one from the second sequence.
Example
let nums =[1,2,3]let animals =["Dog","Cat","Tiger"]let numsAndAnimals =zip(nums, animals)
nomsAndAnimals now contains the following values
sequence1
sequence1
1
"Dog"
2
"Cat"
3
"Tiger"
This is useful when you want to perform some kind of comparation between the n-th element of each Array.
Example
Given 2 Arrays of Int(s)
let list0 =[0,2,4]let list1 =[0,4,8]
you want to check whether each value into list1 is the double of the related value in list0.
let list1HasDoubleOfList0 =!zip(list0, list1).filter {$0!=(2*$1)}.isEmpty
Syntax
Array // The type of an array with elements of type Element
[Element] // Syntactic sugar for the type of an array with elements of type Element
[element0, element1, element2, ... elementN] // An array literal
[Element](http://swiftdoc.org/v2.2/type/Array/#init) // Creates a new empty array of type [Element]