0

Is there any easy way to create MutableProperty from MutableProperty in Swift ReactiveCocoa 4?

I have a case that, and I want an way to create classB with classA, in which I need to setup statusB with statusA, but how to do this?

class ClassA {
  var statusA = MutableProperty<T>
}

class ClassB {
    var statusB = MutableProperty<U>

    func getStatusB(from StatusA: T) -> U {
        // .. assume this is implemented.
    }

    init(statusB: U) {
        //...
    }

    convenience init(from classA: ClassA) {
        self.statusB = // here how to setup this value from classA's statusA with getStatusB(from:)?
    }
}
JerryZhou
  • 4,566
  • 3
  • 37
  • 60

1 Answers1

0

you can't make a MutableProperty<U> directly from a MutableProperty<T> but you can make a MutableProperty<U> with initial value getStatusB(from: classA.statusA.value) and then bind it to classA.statusA.signal.map(getStatusB) so all changes to the MutableProperty<T> propagate to the MutableProperty<U>, like

convenience init(from classA: ClassA) {
    self.init(getStatusB(from: classA.statusA.value)))
    self.statusB <~ classA.statusA.signal.map(getStatusB)
}

(however for this to compile, getStatusB can't be an instance method of ClassB because you need to be able to call it before you call self.init)

Evan Drewry
  • 794
  • 6
  • 17