EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Swift

Swift Arrays

Computer Science / Swift tutorial chapter - Published 2025-12-16 - Swift

An array is a collection that can store multiple values of the same data type. Arrays in Swift are powerful because they allow you to handle collections of data efficiently. For example, if we want to store the marks of multiple students, using a list of variables would not be practical. Instead, an array can hold these marks in a single structure.

Declaring an Array

In Swift, you can declare an array with specific data types. Here’s the basic syntax:

var myArray: [Int] = [10, 20, 30]

Alternatively, Swift can infer the data type:

var myArray = [10, 20, 30]

If you want to create an empty array, use the following syntax:

var myArray: [Int] = []

Initializing an Array with Default Values

Swift allows you to create an array with a default value and a specified size using:

var myArray = Array(repeating: "Swift", count: 3)

This creates an array with three "Swift" values.

Example:

var myArray = Array(repeating: "Swift", count: 3)
print("myArray:", myArray)

Output:

myArray: ["Swift", "Swift", "Swift"]

Accessing and Modifying Array Elements

To access array elements, use the index:

var myArray: [Int] = [10, 20, 19, 29, 45]
print("Element at index 0:", myArray[0]) // Output: 10
print("Element at index 3:", myArray[3]) // Output: 29

To modify array elements, you can use the same indexing:

var myArray: [String] = ["Swift", "Xcode", "iOS"]
myArray[0] = "SwiftUI"
myArray[1] = "Playgrounds"
print("myArray:", myArray)

Output:

myArray: ["SwiftUI", "Playgrounds", "iOS"]

Inserting and Appending Elements

To insert an element at a specific index:

var myArray: [String] = ["Swift", "Xcode"]
myArray.insert("iOS", at: 1)
print("myArray:", myArray)

Output:

myArray: ["Swift", "iOS", "Xcode"]

To add elements to the end of an array:

myArray.append("iPad")
print("myArray:", myArray)

Output:

myArray: ["Swift", "iOS", "Xcode", "iPad"]

Concatenating Arrays

You can concatenate two arrays using the + operator:

var array1: [Int] = [1, 2, 3]
var array2: [Int] = [4, 5, 6]
var combinedArray = array1 + array2
print("combinedArray:", combinedArray)

Output:

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

Iterating Over an Array

To iterate over each element in an array, use a for-in loop:

var myArray = ["Swift", "Xcode", "iOS"]
for element in myArray {
    print(element)
}

Output:

Swift
Xcode
iOS

Counting Elements in an Array

You can easily count the number of elements in an array using the count property:

var myArray: [Int] = [10, 20, 30]
print("Number of elements:", myArray.count)

Output:

Number of elements: 3

Removing Elements from an Array

To remove an element from a specific index:

var myArray: [String] = ["Swift", "Xcode", "iOS", "iPad"]
myArray.remove(at: 2)
print("myArray after removal:", myArray)

Output:

myArray after removal: ["Swift", "Xcode", "iPad"]

If we remove "Apple" from the array:

var myArray: [String] = ["Orange", "Apple", "Banana", "Grapes"]
if let index = myArray.firstIndex(of: "Apple") {
    myArray.remove(at: index)
}
print("myArray after removing 'Apple':", myArray)

Output:

myArray after removing 'Apple': ["Orange", "Banana", "Grapes"]
End of lesson.