3
它們之間有什麼不同?你能給我一個例子,在哪種情況下,我應該使用動態/可變/任何/常量屬性?DynamicProperty vs MutableProperty vs AnyProperty vs ConstantsProperty
它們之間有什麼不同?你能給我一個例子,在哪種情況下,我應該使用動態/可變/任何/常量屬性?DynamicProperty vs MutableProperty vs AnyProperty vs ConstantsProperty
所有的答案都在這個環節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)
}