swiftuitextfieldreactive-cocoa-3

Using combineLatest with UITextFields in RAC 3+


I would like to simply "combine" the signals emitted by a number of textfields and fire a block of code. It seems there are a few ways this "should" work using methods like combineLatest() or the values: SignalProducer initializer. But I am not able to get anything to compile or function as expected.

RAC documentations uses the following example

combineLatest(numbersSignal, lettersSignal)
  |> observe(next: println, completed: { println("Completed") })

But I am not able to compile this kind of usage

I am able to do the following with redundant blocks...

locationTextfield.rac_textSignal().toSignalProducer()
  |> start(next: { txt in
    println(txt)
  })

aircraftTextfield.rac_textSignal().toSignalProducer()
  |>  start(next: { txt in
    println(txt)
  })

I also am not understanding why I need to use toSignalProducer() and start rather than just observing the rac_textsignal itself. This "compiles" but nothing seems to be sent on the signal unless a producer is created and started.

This question/answer ReactiveCocoa combine SignalProducers into one also works, but still seems like a work around, and doesn't explain why signal producers need to be created rather than observing the original rac_textSignal()s


Solution

  • Observing rac_textSignal without transformations is possible, we just need to clarify that rac_textSignal is RACSignal. RACSignal is the ReactiveCocoa 2.0 signal and is related to the Objective-C version. So you need to apply RAC2 operators to such signals, combineLatestWith: could help you to solve such task.

    Transformations are necessary to apply Swift operators due to diff in the basic concept in RAC3. In RAC2 such core entity was RACSignal, against Signal and SignalProducer in the RAC3.