dimanche 14 février 2016

How to check if an object has a stored property?

I need to check if an object's class has a member which could be used for assigning a value. Let's say there's a class:

class MyClass: NSObject {

  var myVar : AnyObject!

  // ...
}

I tried this objC style but properties are a different beast in Swift and second line always returns false:

let myClass: MyClass = MyClass()
let hasClassMember = myClass.respondsToSelector(Selector("setMyVar:"))

if hasClassMember {

  myClass.performSelector("setMyVar:", withObject: "Context" as! AnyObject)
} 

I assume Swift doesn't have a powerful reflection as ObjC has, but is there any solution?





Aucun commentaire:

Enregistrer un commentaire