Overview
- An ordered, random-access collection.
- you use the Array type to hold elements of a single type, the array’s Element type. An array can store any kind of elements—from integers to strings to classes.
1. Creating and Initializing an Array
1.1 Creating an Array
- Swift makes it easy to create arrays in your code using an array literal: simply surround a comma separated list of values with square brackets.
- Without any other information, Swift creates an array that includes the specified values, automatically inferring the array’s Element type.
1.2 init(repeating:count:)
- Creates a new array containing the specified number of a single, repeated value.
1.3 init(_:)
- Creates an array containing the elements of a sequence.
1.4 init()
- Creates a new, empty array.
2. Inspecting an Array
2.1 isEmpty
- A Boolean value indicating whether the collection is empty.
2.2 count
- The number of elements in the array.
2.3 capacity
- The total number of elements that the array can contain without allocating new storage.
3. Accessing Elements
3.1 subscript(_:)
- Accesses the element at the specified position.
3.2 first
- The first element of the collection.
3.3 last
- The last element of the collection.
3.4 subscript(_:)
- Accesses a contiguous subrange of the array’s elements.
4. Adding Elements
4.1 append(_:)
- Adds a new element at the end of the array.
4.2 append(contentsOf:)
- Adds the elements of a sequence or collection to the end of this collection.
4.3 insert(_:at:)
- Inserts a new element at the specified position.
4.4 insert(contentsOf:at:)
- Inserts the elements of a sequence into the collection at the specified position.
4.5 replaceSubrange(_:with:)
- Replaces a range of elements with the elements in the specified collection.
4.6 reserveCapacity(_:)
- Reserves enough space to store the specified number of elements.
5. Removing Elements
5.1 remove(at:)
- Removes and returns the element at the specified position.
5.2 removeFirst(_:)
- Removes the specified number of elements from the beginning of the collection.
5.3 removeLast(_:)
- Removes the specified number of elements from the end of the collection.
5.4 removeSubrange(_:)
- Removes the elements in the specified subrange from the collection.
5.5 removeAll())
- Removes all elements from the collection.
6. Finding Elements
6.1 contains(where:)
- Returns a Boolean value indicating whether the sequence contains an element that satisfies the given predicate.
6.2 first(where:)
- Returns the first element of the sequence that satisfies the given predicate.
6.3 index(of:)
- Returns the first index where the specified value appears in the collection.
6.4 min()
- Returns the minimum element in the sequence.
6.5 min(by:)
- Returns the minimum element in the sequence, using the given predicate as the comparison between elements.
6.6 max()
- Returns the maximum element in the sequence.
6.7 max(by:)
- Returns the maximum element in the sequence, using the given predicate as the comparison between elements.
7. Selecting Elements
7.1 prefix(_:)
- Returns a subsequence, up to the specified maximum length, containing the initial elements of the collection.
7.2 prefix(through:)
- Returns a subsequence from the start of the collection through the specified position.
7.3 suffix(_:)
- Returns a subsequence, up to the given maximum length, containing the final elements of the collection.
7.4 suffix(from:)
- Returns a subsequence from the specified position to the end of the collection.
8. Iterating Over an Array's Elements
8.1 forEach(_:)
- Calls the given closure on each element in the sequence in the same order as a for-in loop.
8.2 enumerated()
- Returns a sequence of pairs (n, x), where n represents a consecutive integer starting at zero, and x represents an element of the sequence.
9. Sorting an Array
9.1 sort()
- Sorts the collection in place.
9.2 sort(by:)
- Sorts the collection in place, using the given predicate as the comparison between elements.
9.3 sorted()
- Returns the elements of the collection, sorted.
9.4 reversed()
- Returns a view presenting the elements of the collection in reverse order.
10. Excluding Elements
10.1 dropFirst()
- Returns a subsequence containing all but the first element of the sequence.
10.2 dropLast()
- Returns a subsequence containing all but the last element of the sequence.
11. Manipulating Indices
11.1 startIndex
- The position of the first element in a nonempty array.
11.2 endIndex
- The array’s “past the end” position—that is, the position one greater than the last valid subscript argument.
12. Splitting and Joining Elements
12.1 split(separator:maxSplits:omittingEmptySubsequences:)
- Returns the longest possible subsequences of the sequence, in order, around elements equal to the given element.
12.2 split(maxSplits:omittingEmptySubsequences:whereSeparator:)
- Returns the longest possible subsequences of the collection, in order, that don’t contain elements satisfying the given predicate.
12.3 joined(separator:)
- Returns a new string by concatenating the elements of the sequence, adding the given separator between each element.
12.4 joined()
- Returns the elements of this collection of collections, concatenated.
13. Comparing Arrays
13.1 ==(_: _:)
- Returns true if these arrays contain the same elements.
13.2 !=(::)
- Returns true if the arrays do not contain the same elements.
13.3 elementsEqual(_:)
- Returns a Boolean value indicating whether this sequence and another sequence contain the same elements in the same order.
13.4 starts(with:)
- Returns a Boolean value indicating whether the initial elements of the sequence are the same as the elements in another sequence.
13.5 lexicographicallyPrecedes(_:)
- Returns a Boolean value indicating whether the sequence precedes another sequence in a lexicographical (dictionary) ordering, using the less-than operator (<) to compare elements.
14. Describing an Array
14.1 description
- A textual representation of the array and its elements.
14.2 debugDescription
- A textual representation of the array and its elements, suitable for debugging.
14.3 customMirror
- A mirror that reflects the array.
15. Bridging Between Array and NSArray
-
When you need to access APIs that require data in an NSArray instance instead of Array, use the type-cast operator (as) to bridge your instance.
-
For bridging to be possible, the Element type of your array must be a class, an @objc protocol (a protocol imported from Objective-C or marked with the @objc attribute), or a type that bridges to a Foundation type.
Next - Set by Example