Get class name of UIViewController in swift

Qadir Hussain picture Qadir Hussain · Oct 14, 2014 · Viewed 37.3k times · Source

How to get class name of UIViewController Class in swift

In Objective C, we can do something like this :

self.appDelegate = (shAppDelegate *)[[UIApplication sharedApplication] delegate];
    UIViewController *last_screen = self.appDelegate.popScreens.lastObject ;

    if(last_screen.class != self.navigationController.visibleViewController.class){

    //.......

}

but in swift I tried like this

let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let last_screen = appDelegate.popScreens?.lastObject as UIViewController

cant do this.

if last_screen.class != self.navigationController.visibleViewController.class{

//....

}

no class method of UIViewController i.e last screen

Answer

juangdelvalle picture juangdelvalle · Feb 19, 2015

To know your class name you can call something like this:

var className = NSStringFromClass(yourClass.classForCoder)