1

I have the below UINib extension method, I was wondering if I can set a delegate for the unarchived view

    public class func decodeView<T:UIView>(nibName name:String,className classType:T.Type,delegate:AnyObject) -> T {

    let nib = UINib(nibName: name)
    let topLevelObjects = nib.instantiateWithOwner(nil, options: nil)
    let view = topLevelObjects[0] as T
    view.setTranslatesAutoresizingMaskIntoConstraints(false)
    //check if view.delegate exists then view.delegate = delegate 
    return view
}
Bobj-C
  • 5,276
  • 9
  • 47
  • 83

1 Answers1

2

If you're asking if Swift supports reflection, TL;DR: you need to subclass from NSObject. Else you get limited info.

In this question, Does Swift support reflection? you get a more detailed discussion about the possibilities you have.

Once you have this part cleared, an example of how to obtain a list of properties can be found in this SO Answer

Although a quick & dirty way could be just to try and access the property (using KVC) and catch the exception if it fails. Swift does NOT support Try/Catch/Finally constructs, but this nice hack allows you to write code like:

SwiftTryCatch.try({
         // try something
     }, catch: { (error) in
         println("\(error.description)")
     }, finally: {
         // close resources
})
Community
  • 1
  • 1
Diego Freniche
  • 5,225
  • 3
  • 32
  • 45