Reactivecocoa: How to start a signal with initial value ?

Created on 20 Dec 2016  路  2Comments  路  Source: ReactiveCocoa/ReactiveCocoa

Hi guys, I want to start a signal with an initial value.

In my case, I just want to send a UIDatePicker as the initial value

// datePicker is of `UIDatePicker`
let dateSignal = datePicker
      .reactive
      .dates
      .map { date in date.timeIntervalSinceNow < 0 }

and I want to have something like this:

// datePicker is of `UIDatePicker`
let dateSignal = datePicker
      .reactive
      .dates
      .map { date in date.timeIntervalSinceNow < 0 }
      .startWith(datePicker) // for demonstrating idea only

However I'm not sure how to approach this. Do I have to make a SignalProducer for my datePicker and then initialise with a initial value ?

I'm using RAC 5.0.0-alpha.5, appreciate for any meaningful responses :)

question

Most helpful comment

A couple of options:

You could upgrade the signal to a producer using SignalProducer(dates) and then use the .prefix(value: initial) operator,

Or you could use a property: Property(initial: date, then: otherDates) (and observe the property's producer if needed).

All 2 comments

A couple of options:

You could upgrade the signal to a producer using SignalProducer(dates) and then use the .prefix(value: initial) operator,

Or you could use a property: Property(initial: date, then: otherDates) (and observe the property's producer if needed).

I wanted to have a SignalProducer instead of a Signal, because I wanted to have the initial value of a text field. I solved it in the following way:

// class property
let cityText: MutableProperty<String> = MutableProperty("")

// view did load
cityText <~ cityTextField.reactive.continuousTextValues.map{ return ($0 ?? "") }

// where i use it (also view did load)
let cityIsNonEmpty = cityText.producer.map{ !$0.isEmpty } // important to use producer here and not signal
ctaButton.reactive.isEnabled <~ cityIsNonEmpty
Was this page helpful?
0 / 5 - 0 ratings