Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 814
Philip Ding Avatar asked Mar 14 '23 15:03

Philip Ding


1 Answers

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)
    }
like image 91
Roman Derkach Avatar answered Apr 24 '23 21:04

Roman Derkach