【发布时间】:2015-11-23 19:10:57
【问题描述】:
我正在编写一个实现KVO 的基类,我想推断该类的dynamic 属性的名称。例如:
class BaseClass {
func beginObserving() {
// What are the dynamic attributes to observe?
// In the case of "SubClass" below, it should be ["name"]
let attributes = ???
for attribute in attributes {
self.addObserver(self, forKeyPath: attribute, options: [.New, .Old], context: &KVOContext)
}
}
}
class SubClass : BaseClass {
dynamic var name: String!
}
List of class's properties in swift 解释了如何使用Mirror(反射)来执行此操作,但它似乎不适用于dynamic vars(即,如果我删除了dynamic 关键字,则链接代码将适用于这种情况)。
【问题讨论】:
标签: reflection swift2