Handle two signals the same way in ReactiveCocoa

344 Views Asked by At

Let's say I have two signals

textField1Signal
|> map { value in
    return value.lowercaseString
}
|> on (
    next: { value in
         println("textField1 changed to \(value)");
    }
)


textField2Signal
|> map { value in
    return value.lowercaseString
}
|> on (
    next: { value in
        println("textField2 changed to \(value)");
    }
)

What I would like to achieve is something like this:

(textField1Signal & textField2Signal)
|> map { value in
    return value.lowercaseString
}
|> on (
    next: { value in
         println("one of the textFields changed to \(value)");
    }
)

The point is I have several Signals and I want to handle all of them the same way. combineLatest: does not work in this case because first of all it is not fired when only one of the signals fires and second of all I would get the values from both textfields and wouldn't know which one actually caused the invocation.

1

There are 1 best solutions below

0
On

With Jakub Vanos help I was able to find the following solution

SignalProducer<SignalProducer<String, NSError>, NSError>(values: [textField1Signal, textField2Signal])
|> flatten(.Merge)
|> map { value in
    return value.lowercaseString
}
|> on (
    next: { value in
        println("one of the textFields changed to \(value)");
    }
)