Understanding Swift Arrays


Introduction to Arrays

Arrays in Swift are powerful data structures used to store ordered collections of values of the same type. Swift enforces strict type checking to prevent unintended data mismatches. If an array is assigned to a variable, it remains mutable, allowing modifications like adding, removing, or changing elements. However, assigning an array to a constant makes it immutable, meaning its size and contents cannot be changed.

Each element in an array is indexed starting from 0, allowing easy access and modification.

Creating Arrays in Swift

Swift provides multiple ways to declare and initialize arrays.

Syntax for Creating an Array

// Explicitly defining an empty array
var numbers: [Int] = []

// Creating an array with inferred type
var fruits = ["Apple", "Banana", "Cherry"]

// Creating an array with repeated values
var repeatedArray = Array(repeating: "Swift", count: 3)


Example:

import Foundation

// Defining arrays using different methods
var array1: [Int] = [10, 20, 30, 40]
var array2 = [5.5, 7.8, 9.6]
var array3 = Array(repeating: "Code", count: 4)

print("Array1: ", array1)
print("Array2: ", array2)
print("Array3: ", array3)

Output

Array1: [10, 20, 30, 40]
Array2: [5.5, 7.8, 9.6]
Array3: ["Code", "Code", "Code", "Code"]

Accessing & Modifying Arrays in Swift

You can access array elements using their index values.

Syntax:

arrayName[index]

Example:

import Foundation

var languages = ["Swift", "Python", "Java"]

// Accessing elements
print("First language: ", languages[0])

// Modifying an element
languages[1] = "C++"
print("Updated Array: ", languages)

Output

First language: Swift
Updated Array: ["Swift", "C++", "Java"]

Adding Elements to an Array

You can add elements to an array using the append() method or the += operator.

Using append()

var numbers = [1, 2, 3]
numbers.append(4)
print(numbers)

//Using += Operator

var names = ["Alice", "Bob"]
names += ["Charlie", "David"]
print(names)

Output

[1, 2, 3, 4]
["Alice", "Bob", "Charlie", "David"]

Iterating Over Arrays

Swift provides multiple ways to loop through arrays.

Using for loop

let numbers = [10, 20, 30, 40]
for num in numbers {
print(num)
}

Output

10
20
30
40


Using enumerated()

let fruits = ["Apple", "Banana", "Cherry"]
for (index, fruit) in fruits.enumerated() {
print("Fruit at index \(index) is \(fruit)")
}

Output

Fruit at index 0 is Apple
Fruit at index 1 is Banana
Fruit at index 2 is Cherry


Using forEach()

let cities = ["New York", "London", "Tokyo"]
cities.forEach { print($0) }

Output

New York
London
Tokyo

Combining Two Arrays

You can merge two arrays using the + operator.

let arr1 = [1, 2, 3]
let arr2 = [4, 5, 6]
let combinedArray = arr1 + arr2
print(combinedArray)

Output

[1, 2, 3, 4, 5, 6]

Important Array Properties

count

Returns the number of elements in an array.

let numbers = [10, 20, 30]
print("Total Elements: ", numbers.count)

//empty
//Checks whether an array is empty.

let emptyArray: [Int] = []
print("Is array empty? ", emptyArray.isEmpty)

Output

Total Elements: 3
Is array empty? true