美文网首页
combine one day

combine one day

作者: 追忆之丘 | 来源:发表于2021-07-18 16:37 被阅读0次

combine

ReplaceNil

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

var subscriptions = Set<AnyCancellable>()

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

ReplaceRmpty

    let empty = Empty<Int, Never>()

    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

      本文链接:https://www.haomeiwen.com/subject/nddapltx.html