That returns a double optional, T??
. You could add ?? nil
to flatten the wrapped optional.
Maybe this does the job?
extension Sequence {
func firstResult<T>(_ predicate: (Element) -> T?) -> T? {
return withoutActuallyEscaping(predicate) { escapingPredicate in
return self.lazy.compactMap(escapingPredicate).first
}
}
func firstOfType<T: AnyObject>() -> T? {
return firstResult { $0 as? T }
}
}