The .first() method returns the first element of the array that fulfills the specified condition. Otherwise, it returns nil.

Note: .first() is a method while .first is a property. Details about the .first array property can be found near the bottom of this entry.


arrayName.first(where: { condition })

The .first() method takes a single condition parameter. It is a closure that returns a boolean value indicating whether a given array element is a match.


var values = [3, 4, 5, 6, 7]
let greaterThanFive = values.first(where: { $0 > 5 })
print(greaterThanFive!) // Force unwrapping optional

In the example above, the .first() method is searching through the values array. The first item that meets the condition of being greater than 5 will be returned.

This will output:


The .first Property

The .first property returns the first element of the array.



Note: The .first property returns an optional value that should be unwrapped. There are different techniques to unwrap optionals.


var numbers = [5, 1, 8, 4]

In the example above, the .first property of the numbers array is printed out. The .first property will return the first item of the numbers array as an optional value. ! forces the expression to unwrap the value of the returned first element.

Note: Force unwrapping optionals in this way is not recommended because it does not handle nil cases.

After unwrapping the value of the returned first element, the output will be:



Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Swift on Codecademy