combine one day

combine

ReplaceNil

在emit element的时候,用来处理nil

var subscriptions = Set()

example(of: "replaceNil") {
    ["A", nil, "C"].publisher.eraseToAnyPublisher().replaceNil(with: "-").sink(receiveValue: { print($0) }).store(in: &subscriptions)
}
/// 输出 A - C

ReplaceRmpty

    let empty = Empty()

    empty.replaceEmpty(with: 1).sink(receiveCompletion: { print($0) }, receiveValue: { print($0) }).store(in: &subscriptions)
    ///输出 1 finished

Incrementally transforming output

scan(::)

scan 有点像reduce

    var dailyGainLoss: Int { .random(in: -10...10) }

    let august = (0..<22).map({ _ in dailyGainLoss }).publisher
    august.scan(50) { latest, current in
        max(0, latest + current)
    }.sink(receiveValue: { _ in }).store(in: &subscriptions)
image.png

你可能感兴趣的:(combine one day)