9

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?

Daniel T.
  • 32,821
  • 6
  • 50
  • 72
mart-thomas
  • 154
  • 1
  • 7

1 Answers1

32

Insert the .dropFirst() operator.

self.isCurrentNameValid
    .dropFirst()
    // ... the rest is as before ...
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 3
    No problem! I got the answer from this free online book: https://www.apeth.com/UnderstandingCombine/operators/operatorsPartitioners/operatorsprefix.html I also _wrote_ it, but I still have to consult it because I can never remember anything. – matt Sep 19 '20 at 21:06
  • 2
    If you already know RxSwift, this will be helpful in learning Combine https://github.com/CombineCommunity/rxswift-to-combine-cheatsheet – Daniel T. Sep 20 '20 at 11:45