DynamicProperty vs MutableProperty vs AnyProperty vs ConstantsProperty

时间:2016-01-09 16:57:13

标签: ios swift reactive-cocoa reactive-cocoa-3

它们之间的区别是什么?你能举个例子说明我应该使用dynamic / mutable / any / constants属性吗?

1 个答案:

答案 0 :(得分:7)

您的所有答案都在此链接中Property.swift

我举几个例子:

let privatString = MutableProperty<String>("PrivatString")
    // AnyProperty are only for observing. You can't change it with observableProperty.value
    let observableProperty: AnyProperty = AnyProperty<String>(privatString)

    print(observableProperty)

    // ConstantProperty describes observable constant value.
    let constantProperty = ConstantProperty<String>("ConstantString")
    //  constantProperty.value = "" Error

    // Thread safe observable mutable property. It's value is changable
    let mutableProperty = MutableProperty<String>("mutableProperty")
    mutableProperty.value = "New mutable property value"

    // DynamicProperty uses KVO. 
    let dynamicProperty = DynamicProperty(object: self.view.layer, keyPath: "bounds")
    dynamicProperty.producer.startWithNext { frame in
        let frame = frame as! NSValue
        let rect = frame.CGRectValue()
        print(rect)
    }