如何从任何多信号制作的触发块?(How to trigger block from any of m

2019-10-29 08:39发布

如何触发一个代码块,只要任何一组SignalProducers的改变? 换句话说,我该如何摆脱我目前的冗余代码:

property1.producer.startWithValues { (value) in 
    // do stuff with property1.value and property2.value
}

property2.producer.startWithValues { (value) in 
    // do the same stuff with property1.value and property2.value
}

Answer 1:

您可以使用combineLatest创建一个包含两个值的新属性:

let prop = property1.combineLatest(with: property2)
prop.producer.startWithValues { (val1, val2) in
    // do stuff here
}

如果任一值的变化,该块将被触发。



Answer 2:

你可以的代码块保存为一个变量,那么你会说简单的可变分配给property1.producer.startWithValues



文章来源: How to trigger block from any of multiple signal producers?