Slice that Collection: A Swift guide

What are slices, and how are they used?

Slices make your use of collections in Swift more efficient. This gives us an opportunity to write efficient code and really get to grips with Indices. Shall we get started, then?

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

  • Some knowledge of Collections in Swift

Terminology

Array: An ordered series of objects which are the same type

Collection: A sequence of elements that can be traversed (as many times as you want) and can be accessed by an indexed subscript

Enumeration: A complete, ordered listing of the items in a collection

Index: This is where files are placed that you want to commit to the git repository. Also known as the staging area.

Indices: The number position given to an Array

Integer: A number that has no fractional part, that is no digits after the decimal point

Offset: In an array or other data structure, the offset is an Integer representing the distance between the beginning of the data structure and the object in question

Set: An unordered collection of values (where all the values are of the same type)

Slice: A slice of a collection that represents a view onto a subsequence of the master collection

Subscript: A shortcut for accessing the members in an array (or any collection or list, depending on the language)

Collections

In Swift, collections (as in other languages) are vitally important. ArraySlices provide a view on an array rather than a new array, meaning that both the initial array and the slice share the same indicies.

This is all covered in my article around ArraySlice, but for review here is a quick summary

The Array Summary

If we have an ages array

let ages = [1,5,12,15,2,26,67,36]

we can take the second and third elements of the ages array, and put it into an arrayslice.

let sliceOfAges = ages[1...2]

however, the indices of sliceOfAges are from the original array meaning the following leads to a rather upsetting error — since we are out of bounds for the array

sliceOfAges[0] // EXC_BAD_INSTRUCTION out of bounds

The Set Summary

For a set we can have the same ages data, but of course the result will be unordered.

let agesSet = [1,5,12,15,2,26,67,36]

If we excuse the rather perfunctory name (sorry) we can expect another ArraySlice to be returned.

let sliceOfAgesSet = agesSet.dropLast()

and indeed the full type is let sliceOfAgesSet: Slice<Set<Int>>

of course the indices of an (unordered of course) set is not as crucial as an Array.

The Dictionary Summary

For a dictionary things are rather familiar after the initilization step

let agesDict : [Int: Int] = [0:1, 1:5, 2:12, 3:15, 4:2, 5:26, 6:67, 7:26]

Again we can use dropLast() to create the arrayslice

let sliceOfDict = agesDict.dropLast()

producing an output of the type:

let sliceOfDict: Slice<Dictionary<Int, Int>>

Conclusion:

ArraySlices are useful, and actually are incredibly similar to Slice which applies to any collection. If you are interested in this wider struct please do read on in my accompanying article on arrayslices. The article? It’s HERE.

Extend your knowledge

  • Apple have a subscript guide (HERE)

The Twitter contact:

Any questions? You can get in touch with me HERE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store