As the error message is clearly stating, you cannot use any instance members within property initializer (= Observer(model?.head)
).
One way to work around this restriction would be moving initialization into init()
.
class SampleViewModel {
var model: SampleModel?
let changeData: Observer<String>
init() {
let model = SampleModel()
self.model = model
self.changeData = Observer(model.head)
}
func changeLabel(_ tf: String) {
self.changeData.value = tf
}
}
You will not see the error with this change, but I'm not sure this would work as you expect.