7 Array Features That Every Swift Developer Should Know

Most useful Array methods to simplify our development code

Mohd Hafiz
Geek Culture
4 min readJun 1, 2021

--

Photo by Jeswin Thomas on Unsplash

Everyone should be familiar with Swift Collection including Array, Set and Dictionary. These three types are very useful in iOS Development to store data in collection form.

In this article, we are going to discuss the useful methods that can be used for Array. Array stores data with the same type in the ordered list and its elements are accessible by an index starting from zero. It can be stored with various data types (Int, String, Double, Any, Optional, etc) and also any type of user-defined Class or Struct.

Basic Syntax

Before we go through each method, let’s revise a bit the basic syntax of Array. See the code below with the explanation in the comments.

Basic syntax of Array

Below is the code showing an Array with objects. We will be working on this sample data for every features that we will discuss. Nice!

Array of Objects

1. Getting First and Last Item

Without these first() and last(), normally we will use index 0 and N-1 to access the first and last element. where N is the total items in Array.

Simple first() and last() example

With Condition

That is simple right. But how if we want to get the first from Array that has “score less than 50". Yes, we are still using first() method but this time with a where closure containing specific condition. We also can apply the same closure condition to the last() method.

Example of method first(where:) and last(where:)

See the code above, $0 is the first shorthand argument pass to the inline closure. In this case, the argument passed is “Student” object. In the next sections, we will keep using the inline closure with $0 and $1.

The shorthand argument is well explained in the Swift documentation.

Swift automatically provides shorthand argument names to inline closures, which can be used to refer to the values of the closure’s arguments by the names $0, $1, $2, and so on.

2. Transforming Array using Map

In this section, we will use map() method. Below are the 3 different Array mapped from the students (Array of Int, String and User).

Map example

3. CompactMap

It is similar to the map method, but it is used when we want to get a result with non-optional items. In the example below, we will get a list of student location. See the output difference between the map() and compactMap().

CompactMap to map and remove nil item

4. Sorting with Condition

In this example, the students Array is initially sorted by score and then map into an array of String (sorted student’s name).

Sort example

5. Filtering

filter() method is used to find match elements based on a given condition inside the closure. Let’s try to get a list of the student with a score above 50. Secondly, we will get a list of male students.

Filter example

6. Getting Highest and Lowest Value

The common way to get the highest or lowest value is by using sort() method with first() or last(). However, Swift provides a simpler solution using max() and min() methods to achieve the similar result.

From the above image, we know that the sorting order is increasing, which means our condition should be written to get values from small to big number.

Get highest and lowest score

7. Remove Array Element with Specific Condition

There are few remove methods provided in Swift for Array. In order to remove an element or multiple elements with specified condition, we can use these two methods.

  1. removeAll(where:)
  2. Longer solution, using firstIndex to find index of the occurrence, then call remove(at:) to remove item at specific index.

Take note that removeAll() will remove all items that meet the condition rather than remove an item.

Photo by bruce mars on Unsplash

Great work everyone!

You may try to implement it in your projects and hopefully, this article will help you to improve your development skills. Thanks for reading and please share the awesomeness with your friends. Feedback is most welcome. Happy coding!

“Learn, that’s how we grow our coding skills.”

References

https://docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html

https://docs.swift.org/swift-book/LanguageGuide/Closures.html

--

--

Mohd Hafiz
Geek Culture

iOS Developer focusing on Swift — “Read, learn & practice!”.