Swift Array Operations [❤]

Batuhan Gobekli
3 min readJan 9, 2020

Today we are covering a simplified topic Array Operations. To start with, we should know what an array is.

Array

According to documentation, arrays identified by the sentence below.

An ordered, random-access collection.

What does it mean? Let’s look at the definition

An ordered

This means that according to a specific rule, the element or elements exist even randomly sequent.

Random Access

You can access any index of an element without moving sequentially, no matter how many items may be in the array.

Why the use of Arrays?

You use arrays to organize your app’s data. Specifically, you use the Array type to hold elements of a single type, the array’s Element type. An array can store any kind of elements—from integers to strings to classes.

How to use it?

Swift makes it easy to create arrays in your code using an array literal: simply surround a comma-separated list of values with square brackets. Without any other information, Swift creates an array that includes the specified values, automatically inferring the array’s Element type. For example:

// An array of 'Int' elements
let numbers = [1, 5, 6, 2, 6]
// An array of 'String' elements
let mixedArray = ["Medium", "is", "Great" ,"1"]
//An array of 'Double' elements
let doubleNumbers = [1.2, 4.3, 6.7, 2.1, 6.9]

How to declare it?

To declare either specifying the type of elements

// An empty array of 'String' elements
var emptyStrings: [String] = []
// An empty array of 'String' elements
var emptyStrings: Array<String> = Array()

How to access items inside?

When you need access element inside array. You can either iterate the array or get the index of the array.

for number in numbers {
print(number)
}
// Prints 1
// Prints 5
// Prints 6
// Prints 2
// Prints 6

Either get the index element of the array. Be sure array contains the selected index element. Or you get an error.

let selectedNumber:Int = numbers[2]
// Prints 6

You can either use “last” or “first” variables of the array to access the element.

let firstIndexElement:Int = numbers.firstlet lastIndexElement:Int = numbers.last

Basic Operations

1. Empty Control

You can use the boolean “isEmpty” variable of the array, whether an array doesn’t’ contains at least one element.

//We declare a variable
var isMyArrayEmpty:Bool = numbers.isEmpty

Now we can use like this

if isMyArrayEmpty {
print("My array is empty")
} else {
print("My array is not empty")
}
// Prints "My array is not empty"

2. Number of elements

The number of items in the array stored in the integer “count” variable.

//We declare a variable
var numberOfElements:Int = numbers.count
print(numberOfElements)//Prints 5

3. Adding/Removing Elements

Suppose you need to store musical instruments in your array. You need to add and remove elements. Your array type as a string.

var insturments = ["guitar", "drum", "violin"]

3.1 Add

Add a single element to your array

insturments.append("trombone")//["guitar", "drum", "violin","trombone"]

Add multiple items to your array

insturments.append(contentsOf: ["bass guitar", "percussion"])//["guitar", "drum", "violin","bass guitar", "percussion"]

Above examples appends the element/s to the end of the array

Add an item to a specific index

students.insert("harp", at: 1)//["guitar","harp", "drum"] //drum index increased 1

3.2 Remove

Remove a single element to your array

insturments.remove(at: 2) //Remove element at index 2//["guitar", "drum"]

Remove the first element to your array

insturments.removeFirst() //Remove element at index 0//["drum"]

Remove the last element to your array

insturments.removeLast() //Remove element at last index//["guitar"]

Remove all elements to your array

insturments.removeAll() //Removes all the elements// []

3.3 Replace

Replace an existing item with a new one

if let i = insturments.firstIndex(of: "drum") {
insturments[i] = "percussion"
}
// ["guitar", "percussion"]

--

--