获取当前值并开始观察MutableProperty

时间:2016-06-16 19:23:28

标签: swift swift2 reactive-cocoa

我有MutableProperty。当我创建新对象时,我想获取MutableProperty的当前值并开始观察。

如:

    let mutableProperty = MutableProperty<Driver?>(Driver(id: 1, name: "John"))
    let label = UILabel()
    label.text = mutableProperty.value?.name

    mutableProperty.signal.observeNext{driver in
        label.text = driver?.name
    }

有可能写得更好吗?

1 个答案:

答案 0 :(得分:3)

如果您想立即使用该值:

let mutableProperty = MutableProperty<Driver?>(Driver(id: 1, name: "John"))
let label = UILabel()

mutableProperty.producer.startWithNext{ driver in
     label.text = driver?.name
}