Swift Combine how to skip an Event

Viewed 4329

I use a form for a logging page in my app and I have a bind on the footer to display any error, as you can see below :

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)
}

The problem is the assign in the viewModel is perform in the init so when I launch my app it will display the message even though the user didn't try to write anything yet.

Is there a way to skip first event like in RxSwift where you would just .skip(1) in combine framework?

1 Answers

Insert the .dropFirst() operator.

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