firstNonNil(_:)

Returns the first non-nil result obtained from applying the given transformation to the elements of the sequence.

FirstNonNil.swift:34
func firstNonNil<Result>(_ transform: (Element) throws -> Result?) rethrows -> Result?

Parameters

transform

A closure that takes an element of the sequence as its argument and returns an optional transformed value.

Returns

The first non-nil return value of the transformation, or nil if no transformation is successful.

let strings = ["three", "3.14", "-5", "2"]
if let firstInt = strings.firstNonNil({ Int($0) }) {
    print(firstInt)
    // -5
}