
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
swift Array first() Function
The first(where:) function of the array in Swift is used to get the first element that satisfies the specified condition from the given array. Here the condition will be represented in the form of closure and the closure will return boolean value. The complexity of this function is O(n), where n represents the length of the array.
For example, we have an array = [4, 9, 2, 5, 19, 11, 0]. Now using the first(where: {$0 < 1}) function we will find the first element that matches the given condition that is {$0 < 1}. So the result is 0.
Syntax
Following is the syntax of the first(where:) function −
func first(where condition: (Self.Element) throws -> Bool) rethrows -> Self.Element?
Parameters
Here condition is a closure that takes each element from the given array as an argument and returns a boolean value. If the given condition matches successful, then it will return true. Otherwise, it will return false.
Return Value
This function returns the first element that matches the given condition. If no element satisfies the given condition then the closure will return nil. So to get the actual value we have to forcefully unwarp the optional value using (!) or if let.
Now we will discuss the use of the first(where:) function with the help of the following examples:
Example 1
Swift program to find the first element from the given array using the first(where:) function.
import Foundation // Array let nums = [2.4, 5.6, 7.8, 9.7, 12.3] // Find the first element from the given array // Using first(where:) function if let firstElement = nums.first(where: { _ in true }) { print("First Element:", firstElement) } else { print("Array is empty.") }
Output
First Element: 2.4
Example 2
Swift program to find the first mark greater than 70 from the given array of the marks using the first(where:) function.
import Foundation // Marks of all the subjects out of 100 let makrs = [45, 60, 59, 66, 78] // Find the first highest mark which is greater than 70 // Using first(where:) function if let firstElement = makrs.first(where: { $0 > 70}) { print("First mark greater than 70:", firstElement) } else { print("Not found") }
Output
First mark greater than 70: 78
Example 3
Swift program to find the name and salary of the employee whose salary is less than 30000 using the last(where:) function.
import Foundation // Structure struct Employee { let name: String let salary: Int } // Array of Employees let emp = [ Employee(name: "Mohan", salary: 30000), Employee(name: "Sonali", salary: 23000), Employee(name: "Sumita", salary: 45000), Employee(name: "Mohit", salary: 50000) ] // Getting the first employee whose salary is less than 30000 // Using first(where:) function if let result = emp.first(where: { $0.salary < 30000}) { print("Employee Name: \(result.name) and Salary: \(result.salary)") } else { print("No employee found") }
Output
Employee Name: Sonali and Salary: 23000
Example 4
Swift program to find the first element from the array whose length is greater than 5 characters using the first(where:) function.
import Foundation // Input array let veggies = ["Potato", "Peas", "Corn", "Cabbage", "Tomato"] // Find the first element with a length greater than 4 characters if let result = veggies.first(where: { $0.count > 5 }) { print("First word with Length greater than 5 characters:", result) } else { print("No word found with length greater than 5 characters.") }
Output
First word with Length greater than 5 characters: Potato