swift Array first Property



An array is a collection that is used to store and manage values of the same types in an ordered list. So using the first property we can find the first element of the specified array. It is a quick-response property and saves us from iterating through each element of the array to find the first element. If the given collection is empty, then this property will return nil. That means this property returns an optional value so to get the actual value we have to forcefully unwrap it using (!) or if let.

For example, we have an array of integer types: Array = [1, 6, 2, 9, 3]. Now using the first property we will find the first element of the array. Hence the first element of the array is 1.

Syntax

Following is the syntax of the first property −

var first: Self.Element? {get}

Return value

This property returns the first element of the array.

Now we will discuss the usage of the first property with the help of the following examples:

Example 1

Swift program to find the first element of the given array using the first property.

import Foundation

// Input array of string type
let arr1 = ["C", "C++", "Java", "Swift", "Python"]

// Finding the first element using the first property
let result1 = arr1.first!
print("First element of \(arr1):", result1)

// Input array of int type
let arr2 = [2, 4, 7, 8, 9]

// Finding the first element using the first property
let result2 = arr2.first!
print("First element of \(arr2):", result2)

// Input array of double type
let arr3 = [2.2, 1.4, 2.37, 2.8, 0.9]

// Finding the first element using the first property
let result3 = arr3.first!
print("First element of \(arr3):", result3)

// Input array of character type
let arr4 = ["A", "B", "C", "D"]

// Finding the first element using the first property
let result4 = arr4.first!
print("First element of \(arr4):", result4)

Output

First element of ["C", "C++", "Java", "Swift", "Python"]: C
First element of [2, 4, 7, 8, 9]: 2
First element of [2.2, 1.4, 2.37, 2.8, 0.9]: 2.2
First element of ["A", "B", "C", "D"]: A

Example 2

Swift program to find the first element of the given array of string type and then reverse the result.

import Foundation

// Function to find the first element of the given array and then reverse it
func reverseFirstItem(array: [String]) -> String {

    // Finding first element 
    let firstItem = array.first!
    
    // Reverse the item using reversed() function
    let revString = String(firstItem.reversed())
    return revString
}


// Input array of string type
let arr = ["Java", "Swift", "Python", "Go"]
print("Original first element: \(arr.first!)")
print("Reversed first element: \(reverseFirstItem(array: arr))")

Output

Original first element: Java
Reversed first element: avaJ

Example 3

Swift program to find the first element of the given two arrays and then find their product.

import Foundation

// Function to find the first element of the given two arrays and 
// then it calculates their sum
func productOfFirstItem(array1: [Int], array2: [Int] ) -> Int{

    // Finding the first element of array1
    let firstItem1 = array1.first!
    
    // Finding the first element of array2
    let firstItem2 = array2.first!
    
    // Calculate the product of the first elements of array1 and array2
    let product = firstItem1 * firstItem2
    return product
}

// Input array of integer type
let arr1 = [3, 42, 21, 6, 77, 12, 1]
let arr2 = [10, 55, 6, 1, 3]

print("Product of first elements: \(productOfFirstItem(array1: arr1, array2: arr2))")

Output

Product of first elements: 30

Example 4

Swift program to find the first element from a two-dimensional array.

import Foundation

//Two-dimensional array of int type
let array: [[Int]] = [
    [2, 4, 6],
    [9, 7, 7],
    [2, 8, 1]]

// Finding the first element 
if let firstRow = array.first, let firstItem = firstRow.first {
    print("First element of 2-D array is \(firstItem)")
} else {
    print("2-D array is empty.")
}

Output

First element of 2-D array is 2
Advertisements