简体   繁体   中英

DynamicProperty vs MutableProperty vs AnyProperty vs ConstantsProperty

What's difference between them? Could you give me an example of in which scenario I should use dynamic/mutable/any/constants property?

All your answer are in this link Property.swift

I give you some examples:

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)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM