【问题标题】:Swift Combine how to skip an EventSwift Combine 如何跳过一个事件
【发布时间】:2020-09-19 20:56:39
【问题描述】:

我在我的应用程序中使用了一个日志页面的表单,并且我在页脚上绑定了显示任何错误,如下所示:

ContentView.Swift:

Form { Section(footer: Text(self.viewModel.errorMessage))

ViewModel.swift

init() {
    self.isCurrentNameValid
         .receive(on: RunLoop.main)
         .map { $0 ? "" : "username must at least have 5 characters" }
         .assign(to: \.errorMessage, on: self)
         .store(in: &cancelSet)
}

问题是 viewModel 中的分配是在 init 中执行的,所以当我启动我的应用程序时,即使用户还没有尝试写任何东西,它也会显示消息。

有没有办法像在 RxSwift 中那样跳过第一个事件,而在组合框架中只需 .skip(1)?

【问题讨论】:

    标签: swift combine


    【解决方案1】:

    插入.dropFirst() 运算符。

    self.isCurrentNameValid
        .dropFirst()
        // ... the rest is as before ...
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多