Easy Tutorial
❮ Swift Continue Statement Swift Variables ❯

Swift Arrays

Swift arrays store multiple values of the same type in an ordered list. The same value can appear multiple times at different positions in an array.

Swift arrays enforce type checking; if the types are mismatched, an error is thrown. Swift arrays should follow the form Array<Element>, where Element is the only type allowed in the array.

If an array is created and assigned to a variable, the collection is mutable. This means that after the array is created, it can be modified by adding, removing, or changing items. If an array is assigned to a constant, the array is immutable, and its size and contents cannot be changed.


Creating Arrays

We can use the initializer syntax to create an empty array of a specific data type:

var someArray = [SomeType]()

The following is the syntax for creating an array with an initial size:

var someArray = [SomeType](repeating: InitialValue, count: NumberOfElements)

The following example creates an empty array of type Int, with 3 elements initialized to 0:

var someInts = [Int](repeating: 0, count: 3)

The following example creates an array with three elements:

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

Accessing Arrays

We can access elements of an array by their index, using the following syntax:

var someVar = someArray[index]

The index starts at 0, so index 0 corresponds to the first element, index 1 to the second element, and so on.

We can learn how to create, initialize, and access arrays through the following example:

import Cocoa

var someInts = [Int](repeating: 10, count: 3)

var someVar = someInts[0]

print("The value of the first element is \(someVar)")
print("The value of the second element is \(someInts[1])")
print("The value of the third element is \(someInts[2])")

The output of the above program is:

The value of the first element is 10
The value of the second element is 10
The value of the third element is 10

Modifying Arrays

You can add elements to the end of an array using the append() method or the += operator. Here, we initialize an array and add elements to it:

import Cocoa

var someInts = [Int]()

someInts.append(20)
someInts.append(30)
someInts += [40]

var someVar = someInts[0]

print("The value of the first element is \(someVar)")
print("The value of the second element is \(someInts[1])")
print("The value of the third element is \(someInts[2])")

The output of the above program is:

The value of the first element is 20
The value of the second element is 30
The value of the third element is 40

We can also modify the value of an array element by its index:

import Cocoa

var someInts = [Int]()

someInts.append(20)
someInts.append(30)
someInts += [40]

// Modify the last element
someInts[2] = 50

var someVar = someInts[0]

print("The value of the first element is \(someVar)")
print("The value of the second element is \(someInts[1])")
print("The value of the third element is \(someInts[2])")

The output of the above program is:

The value of the first element is 20
The value of the second element is 30
The value of the third element is 50

Iterating Over an Array

We can use a for-in loop to iterate over all items in an array:

import Cocoa

var someStrs = [String]()

someStrs.append("Apple")
someStrs.append("Amazon")
someStrs.append("tutorialpro")
someStrs += ["Google"]

for item in someStrs {
   print(item)
}

The output of the above program is:

Apple
Amazon
tutorialpro
Google

If we need both the value and the index of each item, we can use the enumerate() method of String. Here is an example:

import Cocoa

var someStrs = [String]()

someStrs.append("Apple")
someStrs.append("Amazon")

for (index, item) in someStrs.enumerated() {
   print("Item at \(index) is \(item)")
}

The output of the above program is:

Item at 0 is Apple
Item at 1 is Amazon
someStrs.append("tutorialpro")
someStrs += ["Google"]

for (index, item) in someStrs.enumerated() {
    print("The value at index = \(index) is \(item)")
}

The above program execution output is:

The value at index = 0 is Apple
The value at index = 1 is Amazon
The value at index = 2 is tutorialpro
The value at index = 3 is Google

Merging Arrays

We can use the addition operator (+) to merge two existing arrays of the same type. The data type of the new array is inferred from the data types of the two arrays:

import Cocoa

var intsA = [Int](repeating: 2, count: 2)
var intsB = [Int](repeating: 1, count: 3)

var intsC = intsA + intsB

for item in intsC {
    print(item)
}

The above program execution output is:

2
2
1
1
1

count Property

We can use the count property to count the number of elements in an array:

import Cocoa

var intsA = [Int](count: 2, repeatedValue: 2)
var intsB = [Int](count: 3, repeatedValue: 1)

var intsC = intsA + intsB

print("The number of elements in intsA is \(intsA.count)")
print("The number of elements in intsB is \(intsB.count)")
print("The number of elements in intsC is \(intsC.count)")

The above program execution output is:

The number of elements in intsA is 2
The number of elements in intsB is 3
The number of elements in intsC is 5

isEmpty Property

We can use the isEmpty property to check if an array is empty, which returns a Boolean value:

import Cocoa

var intsA = [Int](count: 2, repeatedValue: 2)
var intsB = [Int](count: 3, repeatedValue: 1)
var intsC = [Int]()

print("intsA.isEmpty = \(intsA.isEmpty)")
print("intsB.isEmpty = \(intsB.isEmpty)")
print("intsC.isEmpty = \(intsC.isEmpty)")

The above program execution output is:

intsA.isEmpty = false
intsB.isEmpty = false
intsC.isEmpty = true
❮ Swift Continue Statement Swift Variables ❯