Pieter
Pieter

Reputation: 2691

Swift: use variable for UITableViewController subclass

Take this line of code:

let controller = storyboard.instantiateInitialViewController() as! MyCustomTableViewController

Now I want to replace MyCustomTableViewController by a variable, something like this:

var customTVC: UITableViewController customTVC = MyCustomTableViewController let controller = storyboard.instantiateInitialViewController() as! customTVC

The compiler complains with:

Cannot assign a value of type customTVC.Type to type UITableViewController in coercion

I get the message, but what is the way to do this properly? The obvious point is that the exact sort of subclass can vary and I want to implement that controller reference only once.

Upvotes: 1

Views: 88

Answers (1)

boidkan
boidkan

Reputation: 4731

If I understand your question correctly you can use typealias:

typealias customTVC = MyCustomTableViewController
let controller = storyboard.instantiateInitialViewController() as! customTVC

Take a look at the documentation here under "Type Aliases".

Upvotes: 1

Related Questions