In this tutorial you will learn about the Swift Arrays and its application with practical example.
Swift Arrays
In Swift, an array is ordered collection of homogeneous elements placed in contiguous memory locations that can be accessed individually by adding an index or subscript to a unique identifier. Swift arrays are typed, thus once you declare the type of the array or swift infers it then you would only have elements of the same type. An arrays variable is useful when we want to hold multiple values of single data type in single variable.
- Swift Arrays
- Declaring Array In Swift
- Declaring an empty array
- Declaring an array of specified length with repeated value
- Accessing Array Elements In Swift
- )
- Swift Get Array Elements Count
- Swift Add Elements to an Array (append)
- Swift Add Elements using insert method
- Swift Remove Elements using remove method
- Swift Reverse array elements using reversed method
- Swift Iterating Over an Array Elements
Declaring Array In Swift
Declaring arrays in swift is straightforward, we can simply create an array using a list of values surrounded by square brackets.
Syntax:-
1 |
var <array_name>: [<type>] = [<value, value, ...>] |
Example:-
1 |
var arrayOfIntOne:[Int] = [1,2,3,4,5] |
here, we have created an array named arrayOfIntOne of integers, but if we don’t declare type explicitly then swift automatically infers the type.
Example:-
1 |
var arrayOfIntTwo = [1,2,3,4,5] |
here, we initialized an array named arrayOfIntTwo with value [1,2,3,4,5] and without declaring the type explicitly then Swift automatically infers that it is an integer array.
Declaring an empty array
In Swift, an empty array of a certain type can be created in following ways –
Syntax 1:-
1 |
var <name> = [<type>]() |
Syntax 2:-
1 |
var <name>: [<type>] = [] |
Example:-
1 2 |
let emptyIntArr:[Int] = [] print(emptyIntArr) |
here, we have created an empty array named emptyIntArr of integers, now when we run this program we will see following output –
Output:-
1 |
[] |
Declaring an array of specified length with repeated value
In Swift it is possible to create an array of a given size initialized with a specified value repeated for all elements.
Syntax:-
1 |
var <array_name> = [<type>](repeating: <no_of_elements>, count: <value>) |
Example:-
1 2 |
var arrOfStr = [String](repeating: "W3Adda", count: 3) print(arrOfStr) |
Now, when we run this program we will see following output –
Output:-
Accessing Array Elements In Swift
In swift an array elements can be accessed by using it’s index positions, an array’s index position always starts from zero (0).
Example:-
1 2 3 4 5 6 |
let persons = ["John", "Doe", "Smith", "Alex"] print("W3Adda - Accessing Array Elements Using Index") print(persons[0]) print(persons[1]) print(persons[2]) print(persons[3]) |
Output:-
In swift, we can also use array properties first and last to access first and last elements of given array as following –
Example:-
1 2 3 4 |
let persons = ["John", "Doe", "Smith", "Alex"] print("W3Adda - Accessing Array Elements Using first and last properties") print(persons.first) print(persons.last) |
Output:-
Swift Check Array Empty (isEmpty)
In Swift, isEmpty property can be used to check whether the given array is empty or not.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
let persons = ["John", "Doe", "Smith", "Alex"] var marks:[Int] = [] print("W3Adda - Check empty array using isEmpty property.") if persons.isEmpty{ print("Persons array is empty.") } else{ print("Persons array is not empty.") } if marks.isEmpty{ print("Marks array is empty.") } else{ print("Marks array is not empty.") } |
Output:-
Swift Get Array Elements Count
In swift count property can be used to get the count of elements of given array.
Example:-
1 2 3 |
let persons = ["John", "Doe", "Smith", "Alex"] print("W3Adda - Get array element count.") print(persons.count) |
Output:-
Swift Add Elements to an Array (append)
In Swift append function is used to add or insert an element at the end of the array.
Example:-
1 2 3 4 |
var persons = ["John", "Doe", "Smith", "Alex"] persons.append("Murphy") print("W3Adda - Swift Append an Item.") print(persons) |
Output:-
Swift Add Elements using insert method
In Swift insert function is used to add or insert an element at specified index position of given array.
Example:-
1 2 3 4 |
var persons = ["John", "Doe", "Smith", "Alex"] persons.insert("Murphy", at: 0) print("W3Adda - Swift insert an Item into array.") print(persons) |
Output:-
Swift Remove Elements using remove method
In Swift remove function is used to remove or delete an element from specified index position of given array.
Example:-
1 2 3 4 5 6 7 |
var persons = ["John", "Doe", "Smith", "Alex"] print("W3Adda - Swift remove an array element.") print("Before Delete") print(persons) print("After Delete") persons.remove(at: 2) print(persons) |
Output:-
Swift Reverse array elements using reversed method
In Swift reversed function is used to reverse array elements.
Example:-
1 2 3 4 5 6 7 |
var intArr = [11,12,13,14,15] print("W3Adda - Swift reverse an array element.") print("Before Reverse") print(intArr) print("After Reverse") let reversedArr = Array(intArr.reversed()) print(reversedArr) |
Output:-
Swift Iterating Over an Array Elements
In Swift, we can loop through the array elements using for-in loop as following –
Example:-
1 2 3 4 5 |
var persons = ["John", "Doe", "Smith", "Alex"] print("W3Adda - Iterating Array Elements.") for person in persons { print(person) } |
Output:-