lundi 23 novembre 2015

Listing all `dynamic` variables for a class in Swift2

I'm writing a base class that implements KVO, and I would like to infer the names of the dynamic attributes of the class. For example:

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 explains how to do this with Mirror (reflection), but it does not appear to work for dynamic vars (i.e., if I removed the dynamic keyword the linked code would work for this case).





Aucun commentaire:

Enregistrer un commentaire