Reputation: 9767
I am wanting to get a myClass
value assigned with a type, then instantiate that from the storyboard.
func detailViewControllerClasses(index: Int) -> UIViewController {
let myClass = [CollectionViewController().self, CollectionViewController().self, CollectionViewController().self, CollectionViewController().self][index]
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let vc = storyboard.instantiateViewController(withIdentifier: "CollectionViewController") as? myClass else {
fatalError("Unable to instatiate a ViewController from the storyboard.")
}
}
on the guard let
line the compiler says "undeclared variable myClass". I declared it a few lines up with no errors or warnings. Why is this error happening?
Upvotes: 0
Views: 99
Reputation: 1915
You instantiate your CollectionViewControllers
before accessing .self
. Therefore you are not creating an array of class but an array of instances. Remove the ()
func detailViewControllerClasses(index: Int) -> UIViewController {
let myClass = [CollectionViewController.self, CollectionViewController.self, CollectionViewController.self, CollectionViewController.self][index]
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let vc = storyboard.instantiateViewController(withIdentifier: "CollectionViewController") as? myClass else {
fatalError("Unable to instatiate a ViewController from the storyboard.")
}
}
Upvotes: 1