tryContains(where:)
Publishes a Boolean value upon receiving an element that satisfies the throwing predicate closure.
Declaration
func tryContains(where predicate: @escaping (Self.Output) throws -> Bool) -> Publishers.TryContainsWhere<Self>Parameters
- predicate:
A closure that takes an element as its parameter and returns a Boolean value that indicates whether the element satisfies the closure’s comparison logic.
Return Value
A publisher that emits the Boolean value true when the upstream publisher emits a matching value.
Discussion
Use tryContains(where:) to find the first element in an upstream that satisfies the error-throwing closure you provide.
This operator consumes elements produced from the upstream publisher until the upstream publisher either:
Produces a matching element, after which it emits
trueand the publisher finishes normally.Emits
falseif no matching element is found and the publisher finishes normally.
If the predicate throws an error, the publisher fails, passing the error to its downstream.
In the example below, the tryContains(where:) operator tests values to find an element less than 10; when the closure finds an odd number, like 3, the publisher terminates with an IllegalValueError.
struct IllegalValueError: Error {}
let numbers = [3, 2, 10, 5, 0, 9]
numbers.publisher
.tryContains {
if ($0 % 2 != 0) {
throw IllegalValueError()
}
return $0 < 10
}
.sink(
receiveCompletion: { print ("completion: \($0)") },
receiveValue: { print ("value: \($0)") }
)
// Prints: "completion: failure(IllegalValueError())"