Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Combine how to skip an Event

Tags:

swift

combine

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?

like image 793
mart-thomas Avatar asked Dec 22 '25 05:12

mart-thomas


1 Answers

Insert the .dropFirst() operator.

self.isCurrentNameValid
    .dropFirst()
    // ... the rest is as before ...
like image 66
matt Avatar answered Dec 23 '25 23:12

matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!