【发布时间】:2016-03-08 21:29:06
【问题描述】:
(适用于ReactiveCocoa 4 或者可能是 3)
在我见过的大多数示例和案例中,ReactiveCocoa 对象(如 MutableProperty<TVal, TErr> 或 SignalProducer<TVal, TErr> 涉及将用户界面与数据挂钩)至少在某些 setupBindings 或在构造函数。
我经历过几种情况,当我将对象的声明从范围移动到存储属性时,我的非工作代码突然“正常工作”,反之亦然。例如,在伪代码中:
class Wtf {
// doesn't work
init() {
let prop = MutableProperty<Dah, Dah>()...
doSomethingWith(prop)
}
// also doesn't work
private let prop: MutableProperty<Dah, Dah> = MutableProperty<Dah, Dah>(Dah())
init() {
doSomethingWith(prop)
}
// works?
private let prop: MutableProperty<Dah, Dah>
init() {
prop = MutableProperty<Dah, Dah>(Dah())
doSomethingWith(prop)
}
}
看来有几个基本问题。
给定一些ReactiveCocoa 对象...
- 我什么时候应该声明它是一个属性(
let或var)而不是一个本地实例变量? - 我什么时候应该实例化它作为属性的存储、计算或其他变体与实例
- 什么时候应该是函数
return?
【问题讨论】:
标签: reactive-cocoa reactive-cocoa-3 racsignal