Swift Array Operations [❤]

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


According to documentation, arrays identified by the sentence below.

An ordered, random-access collection.

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

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

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 {
// 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"]

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store