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.