The Guide to Swift Arrays

During your programming adventures you might have stumbled upon the term Array a couple of times. 

What is it and why is it important? To put it simply an Array is a collection of elements that is in an ordered series or arrangement.

It is important because it organizes and compiles data so that it can be easily managed and accessed. To better understand Arrays it is best to go back to the basics.

The basics

Suppose that we have to store data of 5 random samples of a product rating, in our normal way to do things we would probably go about by making 5 separate variables named rating1, rating2, rating3, etc.

This is clearly correct but isn’t it a bit cluttered? It also needs to have all 5 variables declared and may be hard to track in the future.

This is where Arrays come in, instead of the mess above we can simply write it as such:

Wasn’t that a lot easier and cleaner? There is a catch though, Array values can be extracted using indexes, and these indexes always start with 0 and not the usual 1.

A minor sacrifice if you ask me. How about we try printing our data? To do so just write it as

Following the same logic we can then treat our array as if its a normal Int variable like so

Of course like any other variable our array can also handle all data types but just remember that once you assign a data type to it then you should use it all throughout

It is also worth to note that we can also declare an empty array that can be done like so

Useful built-in functionalities

Checking and determining number of content

You can check if an array has no values by using isEmpty. Additionally, you can also check the number of content it has by using count

Accessing First and Last content of the array

You can check the first value of the array by using first. Conversely, you can also check the last value of the array by using last.

Subscript/Slice of the array

You can get a subscript/slice of an array by using the Range operator ()

Adding and Removing elements to the array

Basic functionalities

You can add elements to the array by using append or insert([value], at: [index]). Conversely, you can also remove elements to the array by using remove(at: [index]).

Advanced functionalities

What is interesting to note is that different combinations that you can add and remove data these are
insert(contentsOf:[array], at: [index]) – Instead of just inserting a single value at a specified index we can insert a whole array or range of values inside as well.

replaceSubrange([range from own array], with: [array] – We can also replace a whole range of values with another one.

append(contentsOf: [array]) – We can also add a range of values at the end of an array using

removeFirst() – is to remove the first value of the array 

removeFirst([num]) – is to remove the first [num] of values from the array

Conversely you can also use

removeLast() – is to remove the last value of the array  

removeLast([num]) – is to remove the last [num] of values from the array

removeSubrange([range]) – is to remove elements in a specified range

Checking for data inside the array

contains([element]) – is to check if the element/value is inside the array, returns a Bool

It can also be written as contains(where: [element])

min() – is to get the lowest value in the array. Note that if you compare strings it will get and compare the ASCII value of the letter/string

To check more into ASCII values you can check out this website http://www.asciitable.com/. In our case “A” is min because it has an ASCII value of 65, compared to a and b that has an ASCII value of 97 and 98 respectively.

max() – is to get the highest value in the array. Note that if you compare strings it will get and compare the ASCII value of the letter/string

Miscellaneous uses for data inside the array

sort() – sorts the value of the array. Note that if you compare strings it will get and compare the ASCII value of the letter/string

You can also apply sort in descending using sort(by: > )

reverse() – it reverses the data inside the array

shuffle() – it shuffles the values of the array in a random order

randomElement() – it takes a random element in the array

forEach{ [element] -> Void } – iterates over the array and does specified commands to it

This can be written in another way by utilizing loops

And that’s it! Hope this information will serve you well to better understand and utilize Arrays in the future 🙂

Leave a Comment

Share
Tweet
Pin
Share
Buffer