SoFunction
Updated on 2025-04-11

Guide to how to use arrays in Swift programming

Swift arrays are used to store sequential lists of values ​​of the same type. Swift is to be strictly checked, it does not allow wrong types to be stored in the array.

If an assignment creates an array to a variable, it is always mutable, which means it can be changed by adding elements, deleted or changed its items, but if an array constant is allocated to that array, the array is not changed, that is, its size and content cannot be changed.

Create an array
You can use the following initializer syntax to create an empty array of some type:

Copy the codeThe code is as follows:

var someArray = [SomeType]()

Here is the syntax for creating an array of given size and using initial values:
Copy the codeThe code is as follows:

var someArray = [SomeType](count: NumbeOfElements, repeatedValue: InitialValue)

Here is an example to create an empty array of int type with 3 elements with an initial value of zero:
Copy the codeThe code is as follows:

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

Here is an example of creating an array of three elements and specifying three values:
Copy the codeThe code is as follows:

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

Accessing an array
You can use the subscript syntax to retrieve the corresponding value from the array and pass the value corresponding to the index in the square brackets after the array name, as follows:
Copy the codeThe code is as follows:

var someVar = someArray[index]

Here, the index starts at 0, which means that the first element can be accessed using index 0, the second element can be accessed by using index 1, and others are similar. Let's take a look at the following examples of creating, initializing, and accessing arrays:
Copy the codeThe code is as follows:

import Cocoa

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

var someVar = someInts[0]

println( "Value of first element is \(someVar)" )
println( "Value of second element is \(someInts[1])" )
println( "Value of third element is \(someInts[2])" )


When the above code is compiled and executed, it produces the following results:
Value of first element is 10
Value of second element is 10
Value of third element is 10

Modify the array
You can use the append() method or the addition assignment operator (+=) to add a new item to the end of the array, where you first create an empty array and then add a new element to the array, as shown below:

Copy the codeThe code is as follows:

import Cocoa

var someInts = [Int]()

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

var someVar = someInts[0]

println( "Value of first element is \(someVar)" )
println( "Value of second element is \(someInts[1])" )
println( "Value of third element is \(someInts[2])" )


When the above code is compiled and executed, it produces the following results:
Value of first element is 20
Value of second element is 30
Value of third element is 40

An existing element of the array can be modified by assigning a new value at a given index, as in the following example:

Copy the codeThe code is as follows:

import Cocoa

var someInts = [Int]()

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

// Modify last element
someInts[2] = 50

var someVar = someInts[0]

println( "Value of first element is \(someVar)" )
println( "Value of second element is \(someInts[1])" )
println( "Value of third element is \(someInts[2])" )


When the above code is compiled and executed, it produces the following results:
Value of first element is 20
Value of second element is 30
Value of third element is 50

Iterate/traverse array
You can use for-in to loop iterate over the series. In the following example, the entire set value of the array, as shown in the figure below:

Copy the codeThe code is as follows:

import Cocoa

var someStrs = [String]()

("Apple")
("Amazon")
someStrs += ["Google"]

for item in someStrs {
   println(item)
}


When the above code is compiled and executed, it produces the following results:
Apple
Amazon
Google

You can also use the enumerate() function, which returns the index and the corresponding value as shown in the following example:

Copy the codeThe code is as follows:

import Cocoa

var someStrs = [String]()

("Apple")
("Amazon")
someStrs += ["Google"]

for (index, item) in enumerate(someStrs) {
   println("Value at index = \(index) is \(item)")
}


When the above code is compiled and executed, it produces the following results:
Value at index = 0 is Apple
Value at index = 1 is Amazon
Value at index = 2 is Google

Add two arrays
Use the addition operator (+) to add an array of the same type, which will produce a new array that is from the array that is added and combined with two array values, as follows:

Copy the codeThe code is as follows:

import Cocoa

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

var intsC = intsA + intsB

for item in intsC {
   println(item)
}


When the above code is compiled and executed, it produces the following results:
2
2
1
1
1

count property
You can use read-only calculation (count) array attributes to find the number of elements in the array shown below:

Copy the codeThe code is as follows:

import Cocoa

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

var intsC = intsA + intsB

println("Total items in intsA = \()")
println("Total items in intsB = \()")
println("Total items in intsC = \()")


When the above code is compiled and executed, it produces the following results:
Total items in intsA = 2
Total items in intsB = 3
Total items in intsC = 5

Empty attributes
Use the empty attribute (isEmpty) of a read-only array to find out whether an array is empty, as shown in the figure below:

Copy the codeThe code is as follows:

import Cocoa

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

println(" = \()")
println(" = \()")
println(" = \()")


When the above code is compiled and executed, it produces the following results:
 = false
 = false
 = true