Swift Arrays

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.

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:-

Example:-

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:-

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:-

Syntax 2:-

Example:-

here, we have created an empty array named emptyIntArr of integers, now when we run this program we will see following output –

Output:-

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:-

Example:-

Now, when we run this program we will see following output –

Output:-

swift_array_repeat_value

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:-

Output:-

swift_accessing_array_elements

In swift, we can also use array properties first and last to access first and last elements of given array as following –

Example:-

Output:-

swift_accessing_array_elements_first_last

Swift Check Array Empty (isEmpty)

In Swift, isEmpty property can be used to check whether the given array is empty or not.

Example:-

Output:-

swift_check_empty_array_isempty

Swift Get Array Elements Count

In swift count property can be used to get the count of elements of given array.

Example:-

Output:-

swift_array_count

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:-

Output:-

swift_append_element

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:-

Output:-

swift_insert_element_in_array

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:-

Output:-

swift_array_remove_element

Swift Reverse array elements using reversed method

In Swift reversed function is used to reverse array elements.

Example:-

Output:-

swift_reverse_array

Swift Iterating Over an Array Elements

In Swift, we can loop through the array elements using for-in loop as following –

Example:-

Output:-

swift_iterating_array_elements

In this tutorial we have learn about the Swift Arrays and its application with practical example. I hope you will like this tutorial.