Exploring Swift Collections: In-Depth Guide to Arrays, Sets, and Dictionaries
Tags: ios, mobile, swift
Published: 2023-11-29 05:10:11

---
title: Exploring Swift Collections: In-Depth Guide to Arrays, Sets, and Dictionaries
published: true
date: 2023-11-24 05:16:57 UTC
tags: iOS,Mobile,swift
canonical_url: https://bugfender.com/blog/swift-collections/
cover_image: https://bugfender.com/wp-content/uploads/2023/11/featured-collections_80.webp
---

In Swift there are 3 primary types of collections to store your data in a structured way, namely:

- **[Arrays](#swift-array)**
- **[Sets](#swift-set)**
- **[Dictionaries](#swift-dictionary)**

In this article we aim to give you an overview of each. Specifically we want to show how they’re declared, illustrate the most common operations of each, provide comparisons between them where applicable and highlight the various performance considerations.

This is going to allow you understand how all Swift collection types work and will help you to write efficient code for data handling in Swift.

## Swift Arrays

Arrays in Swift, as in most languages, are representations of lists of items. This is the simplest type of collection, since it merely consists of a group of items put together for convenience.

### Declaring Arrays

There’s several ways to declare an Array in Swift. The most common pattern is to simply wrap the type of array you’re declaring in square brackets, or directly wrap the initial values in Square brackets:

```
let emptyStringArray = [String]()

let stringArray = ["String1", "String2", "String3", "String4"]

```

We can visualise this as a sequence of elements:

![](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-2-1024x278.webp)

While declaring Arrays with values is pretty much always done the same way, empty arrays can be declared in several different ways. For example:

```
let emptyStringArray2: [String] = []

let emptyStringArray3 = Array<String>()

```

### Common array operations

The most common operations when using arrays are:

- Accessing elements
- Insertion
- Remotion
- Sorting
- Iteration through the Array

Now let’s look at each of them, so we can understand their usage:

#### Accessing elements of a Swift array

Arrays are lists. So how do we access elements once we’ve created them?

Well there are two ways. We can simply access them by using their index, which starts from 0, or we can access them directly if they’re the first/last elements:

```
var stringArray = ["1", "2", "3", "4"]

print(stringArray[0]) //This will print "1"
print(stringArray[1]) //This will print "2"
print(stringArray[2]) //This will print "3"
print(stringArray[3]) //This will print "4"
print(stringArray.first) //This will print "1"
print(stringArray.last) //This will print "4"

```

Bear in mind that trying to access an element that does not exist will create an exception and, if it isn’t appropriately dealt with, your app will crash. Therefore, accessing elements without checking if they exist is discouraged. We will delve into this later on.

#### Appending elements to a Swift array

We can add elements simply by using the append function, which adds the elements to the tail of the array:

```
var stringArray = ["1", "2"]
stringArray.append("3")
stringArray.append("4")

print(stringArray) //This will print ["1", "2", "3", "4"]

```

Visually, this is what happens:

![](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-3_90-670x1024.webp)

#### Inserting elements to a Swift array

We can also add elements to specific positions in the array by using insert:

```
var stringArray = ["1", "2"]
stringArray.insert("3", at: 0)
stringArray.insert("4", at: 3)

print(stringArray) //This will print ["3", "1", "2", "4"]

```

Note: When inserting, we can insert into either one existing index, or the next index that may be available.

Taking the example above, if we instead tried to insert “4” at position 7, we would create an exception and our app would crash. So just like with insertions, we should be careful here.

Here’s how these operations look visually:

![](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-4_90-692x1024.webp)

Additionally we can add an entire collection of elements to our arrays:

```
var stringArray = ["1", "2"]
var stringArray2 = ["3", "4", "5"]

stringArray.append(contentsOf: stringArray2)

print(stringArray) //This will print ["3", "1", "2", "4", "5"]

```

![](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-6_90-1024x797.webp)

#### Removing elements from a Swift array

Removing elements is very similar to inserting them. We can do it by their index, or in some cases by their position:

```
var stringArray = ["1", "2", "3", "4", "5"]
stringArray.remove(at: 3)
print(stringArray) //This will print ["1", "2", "3", "5"]

stringArray.removeFirst()
print(stringArray) //This will print ["2", "3", "5"]

stringArray.removeLast()
print(stringArray) //This will print ["2", "3"]

```

The visual representation will look like this:

![](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-5_80-1010x1024.webp)

We can also remove the entire list of elements, clearing the array:

```
var stringArray = ["1", "2"]
stringArray.removeAll()

print(stringArray) //This will print []

```

#### Sorting a Swift array

When it comes to default sorting, there’s two possible ways to do it. We can sort the items within the same array, so won’t have a new variable, but the original array will be modified:

```
var stringArray = ["2", "1", "5", "4", "3"]
stringArray.sort()
print(stringArray) //This will print ["1", "2", "3", "4" "5"]

```

Or we can leave the current array as is, and return a version that is sorted:

```

var stringArray = ["2", "1", "5", "4", "3"]
var stringArray2 = stringArray.sorted()
print(stringArray) //This will print ["2", "1", "5", "4", "3"]
print(stringArray2) //This will print ["1", "2", "3", "4" "5"]

```

Custom classes offer more advanced ways of sorting, and we will explore these in a separate article with more in-depth array operations.

#### Iterating through the array

When we have an array, we can iterate through it using For loops.

There’s a few ways of doing this: in this case, we’ll show you various For variations that allow us to count the amount of “2” instances in an array.

The _For-in_ examines the element’s index, as follows:

```
var stringArray = ["2", "1", "2", "4", "2", "7", "8"]
var twosCount = 0

for index in stringArray.indices {
    if stringArray[index] == "2" {
        twosCount += 1
    }
}

print(twosCount)

```

The _For-in that_ looks at each object itself:

```
var stringArray = ["2", "1", "2", "4", "2", "7", "8"]
var twosCount = 0

for element in stringArray {
    if element == "2" {
        twosCount += 1
    }
}

print(twosCount)//Prints 3

```

The _For-each_ also looks at each object:

```
var stringArray = ["2", "1", "2", "4", "2", "7", "8"]
var twosCount = 0

stringArray.forEach { element in
    if element == "2" {
        twosCount += 1
    }
}

print(twosCount)//Prints

```

By default, when iterating through objects, we will prefer the For-Each. However, if for any reason we’re focused specifically on the element’s index, we can use the For-in.

### Performance of Swift arrays

The performance is **`O(n)`**, since the time it takes to go through an array is linear and directly linked to the Array’s size.

> Explore Swift Closures in-depth in our detailed guide, covering their syntax and practical uses.
> 
> [![](https://bugfender.com/wp-content/uploads/2023/11/featured-closure_400px-1.webp)](https://dev.to/bugfenderapp/swift-closures-explained-a-comprehensive-guide-for-ios-developers-c7d)
> _Swift Closures Explained: A Comprehensive Guide for iOS Developers_

# Swift Sets

Sets have two specific features that distinguish them from an Array:

Ordering – Sets, unlike Arrays, are unordered chunks of data. While an array is easily visualised as a sequence of elements, if we wanted to visualize a set, it would be something like the following:

[![](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-7-1024x807.webp)](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-7.webp)

The elements are randomly present in the set.

Uniqueness: Each element has to be identifiable and unique. This is easily visualised for clarity.

### Initialization and insertion of a Swift set

Now let’s see how we declare a set:

```
var stringSet: Set<String> = ["1", "2", "3"]
print(stringSet) //prints ["1", "2", "3]

```

This creates the following set:

[![](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-8-1024x807.webp)](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-8.webp)

Now that we have the set, let’s insert an element into it:

```
stringSet.insert("4")
print(stringSet) //prints ["4", "2", "1", "3]

```

[![](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-10-944x1024.webp)](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-10.webp)

Our set now looks like this:

[![](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-9_90-1024x807.webp)](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-9_90.webp)

And now lets add another element into it:

```
stringSet.insert("1")
print(stringSet) //prints ["4", "1", "2", "3]

```

[![](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-11-1024x508.webp)](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-11.webp)

This didn’t change our set at all:

[![](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-9_1-1024x807.webp)](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1-9_1.webp)

As we said before, the sets have unique elements. Therefore, by adding another “1”, the set replaces the existing “1” and we still have four elements.

### Common Swift set operations

Operations in sets are way more mathematical than the ones in arrays, and the main ones are as follows:

- Union.
- Symmetric difference.
- Intersection.
- Subtraction.

All of these are easy to understand when we look at them.

```
let stringSet1: Set = ["1", "2", "3", "4", "5"]
let stringSet2: Set = ["1", "4", "cat", "duck", "horse"]
let stringSet3: Set = ["1", "4"]

print(stringSet1.union(stringSet2))
// ["1", "2", "3", "4", "5", "cat", "duck", "horse"]

print(stringSet2.symmetricDifference(stringSet3))
// ["cat", "duck", "horse"]

print(stringSet2.intersection(stringSet1))
// ["1", "4"]

print(stringSet2.subtracting(stringSet1))
// ["cat", "duck", "horse"]

```

### Iterating through a Swift set’s elements

Since sets have no direct method of iteration, any operation of that kind needs to be performed in an array.

You can easily turn a set into an array by doing the following:

```
let stringSet1: Set = ["1", "2", "3", "4", "5"]

let stringAray = stringSet1.sorted()
print(stringArray) //["1", "2", "3", "4", "5"]

```

By converting a set to an array as shown, all operations available to arrays become available.

### Performance of a Swift set

As demonstrated above, we’ll initially need to convert the set to an array in the majority of set access. Thus, just as with arrays, the performance is **`O(n)`**.

> Discover the intricacies of date and time handling in Swift with our article. It provides practical strategies for effectively working with dates, crucial for time-sensitive Swift applications.
> 
> [![](https://bugfender.com/wp-content/uploads/2023/11/featured_80-1-1200x630.webp)](https://dev.to/bugfenderapp/mastering-swift-date-operations-a-comprehensive-guide-for-ios-developers-175a)
> _Mastering Swift Date Operations: A Comprehensive Guide for iOS Developers_

## Swift Dictionaries

Dictionaries are the most flexible data structure that we’re going to discuss. They work as repositories of key-value pairs.

We can see the basic declaration of a dictionary, and how it is represented in memory. Let’s now declare a dictionary that includes integer keys and corresponding string values:

```
let dictionary: [Int: String] = [1 :"one", 2: "two", 3: "three"]

```

In our app’s memory, this is how it will be represented:

[![](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1_90-759x1024.webp)](https://bugfender.com/wp-content/uploads/2023/11/Collections_in_Swift_4db1c784a72c4db09df31b9c2f64aa89Blank_diagram_-_Page_1_90-scaled.webp)

Both the keys and values can be anything, the only limitation for the key is that it is unique. As seen previously, with sets, if we add a duplicate key to a dictionary it will replace the previous key that existed.

So, now that we’ve seen the basics, how do we add/remove elements to a dictionary?

### Adding elements to a Swift dictionary

We simply use the key as we used the array’s index, and give it a value:

```
var dictionary: [Int: String] = [1 :"one", 2: "two", 3: "three"]

dictionary[4] = "four"
dictionary[5] = "five"

print(dictionary)//prints [3: "three", 5: "five", 2: "two", 4: "four", 1: "one"]

```

You can clearly see another similarity with sets: they do not store order.

### Removing elements from a Swift dictionary

Removing elements is very straightforward. We simply need to know which key’s value we want to remove. Then both key and value get removed.

```
var dictionary: [Int: String] = [1 :"one", 2: "two", 3: "three"]

dictionary.removeValue(forKey: 1)
dictionary.removeValue(forKey: 3)

print(dictionary) //prints [2: "two"]

```

### Accessing elements from a Swift dictionary

As is the case with remotion, we can easily access our values with the named keys associated.

```
var dictionary: [Int: String] = [1 :"one", 2: "two", 3: "three"]

print(dictionary[1]) //prints [2: "two"]
print(dictionary[0]) //prints [1: "one"]

```

Here are some other ways of accessing our elements in dictionaries:

It is possible to iterate our dictionaries in a way that is very similar to what we do in arrays, using a For-In.

```
var dictionary: [Int: String] = [1 :"one", 2: "two", 3: "three"]

for (key, value) in dictionary {
    print(key)
    print(value)
}
//This will print:
// 1
// one
// 2
// two
// 3
// three

```

Additionally we can iterate with For cycles through our keys, or our values:

```
var dictionary: [Int: String] = [1 :"one", 2: "two", 3: "three"]

for value in dictionary.values {
    print(value)
}
//This will print:
// one
// two
// three

for key in dictionary.keys {
    print(value)
}
//This will print:
// 1
// 2
// 3

```

### Performance of dictionaries in Swift

Accessing a dictionary directly is as performant as can be, so it takes **`O(1)`** time.

On the other hand, accessing elements through cycles relies on making the keys/values into arrays, and therefore the performance is the same as regular arrays, namely **`O(n)`**.

## Swift Collections FAQ

### What are the primary collection types in Swift?

Swift offers three main collection types: **Arrays** , **Sets** , and **Dictionaries** , each serving distinct purposes in data storage and manipulation.

### How do you declare and initialize an Array in Swift?

Arrays in Swift can be declared using square brackets around the type or directly around initial values. For example: `let emptyStringArray = [String]()` or `let stringArray = ["String1", "String2"]`.

### What is the main difference between Sets and Arrays in Swift?

The key difference is that Sets are unordered and each element must be unique, while Arrays are index ordered collections where duplicate values are allowed.

### How do you add and remove elements in a Swift Dictionary?

Elements can be added to a Dictionary by assigning a value to a unique key, and removed using the `removeValue(forKey:)` method.

### Can Swift Arrays contain elements of different types?

By default, Swift Arrays are homogenous. However, you can store multiple types in an array using type casting or by defining the array to store elements of a common superclass or protocol.

### How can you handle optionals while accessing Dictionary values in Swift?

When accessing values from a Dictionary, you often deal with [optionals](https://developer.apple.com/documentation/swift/optional). Use optional binding or the nil-coalescing operator to safely handle these cases.

### How do you handle errors when accessing array elements by index in Swift?

Accessing an array with an index out of its bounds can cause a runtime error. To handle this safely, you should check that the index is within the array’s count or use methods that return an optional.

> Even as an iOS developer, exploring Android’s capabilities can be helpful. Gain insights into Kotlin Collections and learn how to use it for your Android app development.
> 
> [![](https://bugfender.com/wp-content/uploads/2023/11/Kotlin-connections.webp)](https://dev.to/bugfenderapp/leveraging-kotlin-collections-in-android-development-2m3m)
> _Leveraging Kotlin Collections in Android Development_

## To Sum Up

There are three distinct types of Collections natively in Swift. They all have their purpose and should be used appropriately throughout development.

To finish, we will now try to help you understand what to choose and when, based purely on your needs. To identify the most appropriate collection, start by asking yourself a few questions:

- _Do I just need a simple container in which to store values?_ Array.
- _Do I need to make sure all values are unique?_ Set.
- _Do I have a straightforward way of mapping each element to a certain key?_ Dictionary.

It’s really that simple. So happy coding guys!