Array and ArraySlice in Swift 3

In this post, we will learn how to grab sub arrays out of arrays in Swift — and they are considered type ArraySlice.

According to Apple, ArraySlice is:

A slice of an Array, ContiguousArray, or ArraySlice instance.
The Array Slice type makes it fast and efficient for you to perform operations on sections of a larger array. Instead of copying over the elements of a slice to new storage, an Array Slice instance presents a view onto the storage of a larger array. And because Array Slice presents the same interface as Array, you can generally perform the same operations on a slice as you could on the original array.

Interesting, hmm?

A picture is worth a thousand words — let’s jump right onto the code examples. ( Ha! It’s not a picture tho : P )

let arrayOfInts = [ 0, 1, 2, 3, 4, 5 ]
let subArray = array[0...3]
print(subArray) // [ 0, 1, 2, 3 ] is printed!

The code above is self explanatory — we are slicing out numbers from the index 0 to 3 from arrayOfInts and put them into a constant called subArray.

It looks like subArray is just like a type of Array… but is it true? Let’s run the following method in the Playground to find out! We are going to get the sum of all numbers in arrayOfInts.

let arrayOfInts = [ 0, 1, 2, 3, 4, 5 ]

func sum(array:[Int]) -> Int {
return array.reduce(0, +)
}
print(sum(array: arrayOfInts)) // 15 is printed!

Here, I used reduce, a higher order function frequently used in Swift. If you are not sure how to use it in code, please take a look at my post about higher order functions here.

But for your convenience, here is the for loop version of the above function as well.

func sum(array:[Int]) -> Int {
var sum = 0
     for num in array {
sum += num
}
     return sum
}
print(sum(array: arrayOfInts)) // 15 is printed!

Again, this function is self explanatory — we are adding every element in the array to a variable called sum and return the value.

Now let’s call this function by passing the subArray we created right above into the argument.

sum(array:subArray) // Ouch - ERROR! See below!

Wait, what just happened? Right — it threw an error in the red area.

It just turned out that Array and ArraySlice are two different Swift types. Remember, Swift is a type safe language!

Unless we make a new instance of Array using the ArraySlice instance we have — subArray, the function won’t work — but we Swift developers do not want to allocate a precious memory to a new guy.

To work around this, we will use a generic function that limits every element of the passing type to be Int. Let’s modify the function we wrote above to be generic so that we can use both arrayOfInts AND subArray smoothly in the function.

func sum<T: Sequence>(numbers: T) -> Int where T.Iterator.Element == Int {
var sum = 0
     for num in numbers {
sum += num
}
     return sum
}

Let’s call the function… and the result you see is!

Happy Ending — isn’t it?

We learned two things —One, there’s a type called ArraySlice in Swift — how to use it is completely up to you and apparently, it is not type Array so use it with caution. Two, generics in Swift can help us write flexible and reusable functions that accept certain or any type as a parameter. As generics are slightly out of scope here, I suggest you to learn more about them by visiting Bob Lee’s Intro to Generics in Swift with Bob Bob Lee.

Happy Coding!