他们之间有什么区别?你能给我一个例子,说明我应该在哪种情况下使用动态/可变/任何/常量属性?
问问题
1092 次
1 回答
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)
}
于 2016-01-14T17:38:45.820 回答