在ReactiveCocoa

时间:2015-08-05 08:35:52

标签: ios signals reactive-cocoa frp reactive-cocoa-3

我们说我有两个信号

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)");
    }
)

我想要达到的目标是:

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

关键是我有几个Signals,我想以同样的方式处理所有这些。 combineLatest:在这种情况下不起作用,因为首先它只在其中一个信号触发时才触发,其次我会从两个文本字段中获取值并且不知道哪一个实际上导致了调用。

1 个答案:

答案 0 :(得分:0)

借助Jakub Vanos的帮助,我找到了以下解决方案

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)");
    }
)