【发布时间】:2017-10-12 10:22:36
【问题描述】:
具有多个从视图和其他类更新的变量的结构
struct MyViewViewModel {
let style: Variable<CustomEnum> = Variable(.value1)
let displayedValue: Variable<String> = Variable("")
let stepIndex = Variable(0)
let startedDate: Date
var disposeBag = DisposeBag()
}
style - 从第三部分类更新
stepIndex - 应该从 View 的 CocoaAction 更新并依赖于 style 更改(重置)
displayedValue - 取决于style + stepIndex + startedDate
init(style: Observable<CustomEnum>, startedDate: Date) {
self.startedDate = startedDate
style
.bind(to: self.style)
.disposed(by: disposeBag)
self.style.asObservable()
.map { _ in return 0 }
.bind(to: stepIndex)
.disposed(by: disposeBag)
self.style.asObservable()
.map { return self.string(for: $0) } //error: Closure cannot implicitly capture a mutating self parameter
.bind(to: displayedValue)
.disposed(by: disposeBag)
}
mutating func string(for type: CustomEnum) -> String {
return "\(self.stepIndex.value) + \(type) + \(self.startedDate.day)"
}
主要问题如何处理stepIndex变量首先和更新startedDate值之后。
我不想使用类代替结构我不确定它是否如此需要(以避免保留循环)。 RxSwift 应该有办法解决这个问题。
【问题讨论】:
-
你对这里的班级有什么看法? RxSwift
Variable本质上不是值类型。为什么func string(for type: CustomEnum) -> String会发生变异?
标签: swift struct observable rx-swift frp