ios - Observing object properties with ReactiveCocoa 4 in Swift -
as preface, might incredibly simple and/or ignorant question.
in reactivecocoa 2.x, able use racobserve , rac observe properties of object. documentation can find in reactive 3 , 4, use propertytype observe changes object property. have far been unsuccessful in being able observe property change when using mutableproperty or dynamicproperty.
class tempobject { var property: string } let tempobject = tempobject() let propertyobserver: mutableproperty<string> = mutableproperty(tempobject.property)
from understand, should able use propertyobserver view changes tempobject.property. tried adding map function signal producer propertyobserver see if firing, don't see when updating tempobject.property. again, trivial thing missing, much.
edit
nachosoto nailed - needed make property kvo compliant. ended doing this:
let tempobjectsignal: mutableproperty<tempobject> = mutableproperty(tempobject) let propertyobserver: mutableproperty<string> <~ tempobjectsignal.producer.map({ $0.property })
and whenever tempobject.property updated make sure call
tempobjectsignal.value = tempobject
this fires off necessary signals. don't know if breaks best practices, though. let me know think!
mutableproperty(value)
creates mutable property but value
initial value.
what want use dynamicproperty
, use objective-c runtime , kvo
detect changes object's property:
let property = dynamicproperty(tempobject, "property")
for reason, however, need make sure property want observe part of objective-c runtime, making class subclass of nsobject
, , either using dynamic
keyword:
class tempobject: nsobject { dynamic var property: string }
or using @objc
ensure gets exported runtime:
class tempobject: nsobject { @objc var property: string }
Comments
Post a Comment