Contents

ArraySlice In Swift


Array is a very common data collection. We use it pretty much every day. Array in Swift is very powerful and provides several features. In this article, we’ll discuss one of them: ArraySlice.

Introduction

Apple describes ArraySlice as “Views onto Arrays”. It means that an ArraySlice is an object which represents a subsequence of its array. It’s very powerful and allows us to perform the same array operations on a slice like append, map and so on.

In this article, we will see how to create it and some internal behaviors to avoid troubles.

Happy Reading!

How to create an ArraySlice

Before using an ArraySlice, we must understand how to create it. Swift provides several ways to get a slice from a given array:

drop

Array provides some methods to create a subsequence removing some elements. These methods are dropFirst, dropLast and drop:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
let array = [5, 2, 10, 1, 0, 100, 46, 99]

// Drops first
array.dropFirst() // ArraySlice<Int>[2, 10, 1, 0, 100, 46, 99]

// Drops first three elements
array.dropFirst(3) // ArraySlice<Int>[1, 0, 100, 46, 99]

/ Drops last
array.dropLast() // ArraySlice<Int>[5, 2, 10, 1, 0, 100, 46]

// Drops last three elements
array.dropLast(3) // ArraySlice<Int>[5, 2, 10, 1, 0]

// Drops all the elements less than 15
array.drop { $0 < 15 } // ArraySlice<Int>[100, 46, 99]

prefix

If we want to create a subsequence with the first elements of a given array, we can use the method prefix:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
let array = [5, 2, 10, 1, 0, 100, 46, 99]

// First 4 elements
array.prefix(4) // ArraySlice<Int>[5, 2, 10, 1]

// First elements until the index 4, excluding the element at index 4
array.prefix(upTo: 4) // ArraySlice<Int>[5, 2, 10, 1]

// First elements until the index 4, including the element at index 4
array.prefix(through: 4) // ArraySlice<Int>[5, 2, 10, 1, 0]

// First elements until the condition fails (in this case, the elements must be less than 10)
array.prefix { $0 < 10 } // ArraySlice<Int>[5, 2]

suffix

suffix has the opposite behavior of prefix. It returns the last elements of the array:

1
2
3
4
5
6
7
let array = [5, 2, 10, 1, 0, 100, 46, 99]

// Last 3 elements
array.suffix(3) // ArraySlice<Int>[100, 46, 99]

// Last elements from the index 5, including the element at index 5
array.suffix(from: 5) // ArraySlice<Int>[100, 46, 99]

Range

We can create an ArraySlice using also a range of indices:

1
2
3
4
5
6
7
let array = [5, 2, 10, 1, 0, 100, 46, 99]

// From index 3 to index 5
array[3...5] // ArraySlice<Int>[1, 0, 100]

// From index 3 to index less than 5
array[3..<5] // ArraySlice<Int>[1, 0]

With Swift 4, we can omit the startIndex if want to use the index 0 and the endIndex if we want to use the last index of the array:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
let array = [5, 2, 10, 1, 0, 100, 46, 99]

// From index 0 to index 2
array[...2] // ArraySlice<Int> [5, 2, 10]

// From index 0 to index less than 2
array[..<2] // ArraySlice<Int> [5, 2]

// From index 6 to index 7
array[6...] // ArraySlice<Int> [46, 99]

// From index 0 to index 7
array[...] // ArraySlice<Int> [5, 2, 10, 1, 0, 100, 46, 99]

Convert ArraySlice to Array

Let’s suppose that we want to remove the last 5 elements from an array. We can use the method dropLast(5) and assign the result to the array:

1
2
3
4
var array = [5, 2, 10, 1, 0, 100, 46, 99]

let slice = array.dropLast(5)
array = slice // Cannot assign value of type 'ArraySlice<Int>' to type '[Int]'

Unfortunately, Swift doesn’t allow to assign an ArraySlice object to an Array. For this reason, in example above, we have a compile error Cannot assign value of type 'ArraySlice<Int>' to type '[Int]'. We need a way to convert the ArraySlice object to Array. Fortunately, Swift allows us to cast the slice variable using the syntax Array(<slice_variable>). We can use this cast to fix the example above like this:

1
2
3
4
var array = [5, 2, 10, 1, 0, 100, 46, 99]

let slice = array.dropLast(5)
array = Array(slice) // [5, 2, 10]

Relation between ArraySlice and its Array

Strong reference

ArraySlice holds a strong reference of its array. It means that we should pay attention how we use the slice to avoid troubles with the memory. Apple added also a warning about it in the documentation:

Long-term storage of ArraySlice instances is discouraged. A slice holds a reference to the entire storage of a larger array, not just to the portion it presents, even after the original array’s lifetime ends. Long-term storage of a slice may therefore prolong the lifetime of elements that are no longer otherwise accessible, which can appear to be memory and object leakage.

Cit.

We can test this behavior with the following example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
final class Element {
    deinit {
        print("Element deinit is called")
    }
}

final class ArrayHandler {
    let array = [Element(), Element()]
}

final class Main {
    var slice: ArraySlice<Element>
    
    init() {
        let arrayHandler = ArrayHandler()
        slice = arrayHandler.array.dropLast() // `slice` holds a strong reference of `arrayHandler.array`
    }
}

let main = Main()

If we let run this example, we can notice that the property slice holds a strong reference of the array elements. Even though arrayHandler is destroyed at the end of the init scope, the deinit method of Element is not called.

Consequences of ArraySlice and Array changes

Let’s suppose that we have an Array and an its ArraySlice. If we change the elements of the Array, the ArraySlice won’t be affected and will continue storing the original elements:

1
2
3
4
5
6
var array = [10, 46, 99]
let slice = array.dropLast() // ArraySlice<Int> [10, 46]
array.append(333) // [10, 46, 99, 333]

// `slice` is not affected and continues storing the original elements
print(slice) // ArraySlice<Int> [10, 46]

Vice versa, if we change the elements of an ArraySlice, its Array won’t be affected:

1
2
3
4
5
6
let array = [10, 46, 99]
var slice = array.dropLast() // ArraySlice<Int> [10, 46]
slice.append(333) // ArraySlice<Int> [10, 46, 333]

// `array` is not affected and continues storing the original elements
print(array) // [10, 46, 99]

Slice Indices

ArraySlice maintains the same indices of its array. It means that the element at index 3 of an ArraySlice is the same element at the index 3 of its original array.

Let’s consider an ArraySlice with a range 2...4. If we try reading the element at index 0 of the slice, we would have a run time error:

1
2
3
4
let array = [10, 46, 99, 00, 6]
var slice = array[2...4] // ArraySlice<Int> [99, 0, 6]

slice[0] // fatal error: Index out of bounds

It happens because the slice doesn’t contain the element at index 0 of the array but only the elements at index 2, 3 and 4.

If we want to get the first and last index of a slice in a safe way, we should use startIndex and endIndex:

1
2
3
4
5
let array = [10, 46, 99, 00, 6]
var slice = array[2...4] // ArraySlice<Int> [99, 0, 6]

slice[slice.startIndex] // 99
slice[slice.endIndex - 1] // 6

endIndex returns the position one greater than the last valid index. For this reason, we must use endIndex - 1 in the example above to avoid an Index out of bounds error.

Conclusion

In addition to ArraySlice, Swift provides also a base object Slice. It allows us to get a subsequence of other collection objects like Dictionary, Set and any kind of custom collection.