Home > AI > IOS > Combine >

tryMax(by:)

Example (Apple Official):

struct IllegalValueError: Error {}


let cancellable = [0, 10, 6, 14, 22, 22].publisher
    .tryMax { first, second -> Bool in
        if (first % 2 != 0) {
            throw IllegalValueError()
        }
        return first < second // return max
//        return first > second // return min
    }
    .sink(
        receiveCompletion: { print ("completion: \($0)") },
        receiveValue: { print ("value: \($0)") }
    )

Leave a Reply