Javatpoint Logo
Javatpoint Logo

Swift Arrays

Arrays are used to store multiple values of same data type. An Array is just like a container which holds multiple data/ values of a data type in an ordered list. When you fetch the data, you will get the elements in the same order as you defined in the array.

An array can store values of any data type e.g. Int, String, class etc.

How to declare array in Swift 4?

Specify the data type inside the square brackets [] to create an empty array. You have to specify the type inside the square bracket [], otherwise Swift will treat it as a normal data type and you can store only a single value in it.

Declare an empty array

Syntax:

Or

Output:

[]

Here, we have declared a constant emptyIntArr that can store array of integer and initialized with 0 values.

Create an array directly

Swift is a type inference language so, we can also create an array directly without specifying the data type but we have to initialise with some values so that compiler can finds out its type.

Example:

Output:

[1, 2, 3, 4, 5]

Here, we have declared a constant someIntArr that can store array of Integer without specifying the type explicitly. Also, we have initialized array which gives the output as 1, 2, 3, 4, 5.

Declare an array with specified number of single repeated value

We can repeat a value to specific number of times to make an array in Swift. It is done by using the array initializer with repeating and count.

Example:

Output:

["JavaTpoint", "JavaTpoint", "JavaTpoint", "JavaTpoint", "JavaTpoint"]

Here, we have defined a constant arrWithRepeatingValues that stores an array of string JavaTpoint and repeats the same value for 5 times as specified in the count.

Note: In Swift 4, we cannot create an array of fixed length size as we can do in other programming languages.

How to store values in Array?

Let's take a constant IntArray to store some strings:

Swift Arrays

The index of an array starts with 0 means the first element is stored in index 0, second element in index (1) and so on.

How to access array elements in Swift?

You have to use index of the value of that array you want to access within square brackets immediately after the name of the array.

Suppose, we declared an array intArray as above. The first element is intArray[0], second element is intArray[1] and so on. We use them to access the array's values.

Example:

Output:

10
11
12
13
14

How to modify elements in Array?

We can modify elements of the array by using assignment operator. We have to add the index of the value we want to update within square brackets after the name of the array followed by the assignment operator and new value.

Example:

Output:

[12, 42, 45, 36, 14]

You can also modify the whole array completely.

Example:

Output:

[10, 20, 30, 40]

A list of built in Array functions and their properties:

Index Array function Explanation
1. isEmpty The isEmpty property is used to determine wheather an array is empty or not. It returns true if the array does not contain any value otherwise, returns false.
2. first This property is used to access first element of an array.
3. append The append function is used to insert/append element at the end of the array.
4. insert This function is used to insert/append element at specific index of the array.
5. remove This function removes and returns the value specified at the specific position from the array.
6. reversed This function returns the elements of array in reverse order.
7. count This property returns the total number of elements in an array.

Next TopicSwift Dictionary




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA