jeudi 4 mai 2017

Getting the value of a property using it's string name in pure Swift using reflection

I want to use Swift (not Objective-C runtime) Reflection to create a method like this:

func valueFor(property:String, of object:Any) -> Any? {
    ...
}

To some extent, I can do this using:

func valueFor(property:String, of object:Any) -> Any? {
    let mirror = Mirror(reflecting: object)
    return mirror.descendant(property)
}

With

class TestMe {
    var x:Int!
}

let t = TestMe()
t.x = 100
let result = valueFor(property: "x", of: t)
print("\(result); \(result!)")

This prints out what I'd expect:

Optional(100); 100

When I do:

let t2 = TestMe()    
let result2 = valueFor(property: "x", of: t2)
print("\(result2)")

The output is:

Optional(nil)

This might seem reasonable, except that if I do:

var x:Int!
print("\(x)")

This prints out:

nil

and not Optional(nil). The bottom line is that I'm having difficulty programmatically determining that the value of t2.x is nil using my valueFor method.

If I continue the above code with:

if result2 == Optional(nil)! {
    print("Was nil1")
}

if result2 == nil {
    print("Was nil2")
}

Neither of these print statements output anything.

When I put a breakpoint into Xcode and look at the value of result2 with the debugger, it shows:

▿ Optional<Any>
  - some : nil

So, my question is: How can I determine if the original member variable was nil using the result from valueFor?





Aucun commentaire:

Enregistrer un commentaire