Swift Integer Array

In this tutorial, we will learn about Swift Integer Arrays.

Arrays are important derived datatypes in any functional programming language. Arrays help to store similar type of objects under a single variable name while providing access to the objects using an index.

While getting started with Integer Arrays in Swift programming, let us see the syntax to create an empty array.

Create an empty Integer Array

To create an empty Integer array, use the following syntax.

var array_name = [Int]()

array_name is used an identifier to access this Integer array.

[Int] specifies that this is an array which stores Integers.

There is no need to mention the size of an array while creation. The size of an empty Integer array is 0.


Create an Integer Array with a default value

Swift allows creating an Integer array with a specific size and a default value for each integer in the array.

To create an Integer array with a specific size and default value, use the following syntax.

var array_name = [Int](count: array_size, repeatedValue: default_value)


array_size is an integer that defines the size of this array.

default_value is the default value for each integer in this array.

An example would be

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

In the above statement, an integer array would be created with a size of 3 and a default value of 10 for each item in the array.

Create an Integer array with initial values

You can also create an Integer array in Swift with initial values. Which means you can combine array declaration and initialization in a single statement.

var array_name:[Int] = [7, 54, 21]

Note that there is a slight variation in the syntax. The [Int] part comes to the left side.

The initial values are enclosed in squared brackets and are separated by a comma.

Access Integer Array in Swift

Items of the Integer Array can be accessed using the index.


var numbers:[Int] = [7, 54, 21]

var a = numbers[1]

print( "Value of integer at index 1 is \(a)" )


Value of element at index 1 is 54

Note: The index of an array starts at 0.

From the above example, numbers[0] = 7, numbers[1]=54 and so on.

Following are some the operations that can be performed on Integer Arrays in Swift.


In this Swift Tutorial, we have learned to declare, initialize, access and other operations on Integer Arrays in Swift programming.