lundi 4 janvier 2016

How can I check if a property has been set using Swift reflection?

Some of my models have optional properties. I'm trying to write a method that can evaluate if they've been set.

Below is an attempt, but I can't figure out how to determine a nil value from an Any object. It doesn't compile.

func allPropertiesHaveValues(obj: AnyObject) -> Bool {
    let mirror = Mirror(reflecting: obj)
    for child in mirror.children {
       let value = child.value
       if let optionalValue = value as? AnyObject? { //Does not compile
          if optionalValue == nil {
             return false 
          }
       }
    }
    return true
}





Aucun commentaire:

Enregistrer un commentaire