0

There is this code:

self.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

var exampleViewController: CustomClass1 = mainStoryboard.instantiateViewController(withIdentifier: "custom") as! CustomClass1

self.window?.rootViewController = exampleViewController        
self.window?.makeKeyAndVisible()

I want to create a function that has this code inside it, but the problem is I want to pass a class as parameter so that if I pass CustomClass2, the middle line of the code should work like

var exampleViewController: CustomClass2 = mainStoryboard.instantiateViewController(withIdentifier: "custom") as! CustomClass2

the function should be called like this: foo(class: CustomClass3)

How can I write this function?

PS: I'm not sure if this makes difference but all these CustomClasses are Custom ViewControllers.

  • 1
    Do you really need to specify the class? If you have the correct class set in the storyboard for each view controller and match it with the right identifier, Swift type inference should work. No type declaration, no `as!`.... Basically, it would turn into `func foo(identifier: String)`. – Phillip Mills Mar 14 '17 at 19:59
  • Good point @PhillipMills, in fact I can write a workaround for that, but for sake of knowledge I'd like to know anyway :) –  Mar 14 '17 at 20:01
  • See http://stackoverflow.com/q/33200035/2976878 – Hamish Mar 14 '17 at 21:22

0 Answers0