'Get class name of UIViewController in swift

How do you get the class name of a 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:

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

Can't do this.

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

//....

}

no class method of UIViewController i.e last screen



Solution 1:[1]

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

var className = NSStringFromClass(yourClass.classForCoder)

Solution 2:[2]

The cleanest way without needing to know the name of your class is like this.

let name = String(describing: type(of: self))

Solution 3:[3]

A simple way in swift 3 is to write the below code:

for instances:

let className = String(describing: self)

for classes:

let className = String(describing: YourViewController.self)

Solution 4:[4]

Expanding on juangdelvalle's answer.

I added this as an extension so that it's reusable and easier to call from any view controller. Also in some cases NSStringFromClass in Swift returns a string in the format like this:

< project name >.viewControllerClassName.

This extension property is modified to get rid of the project name prefix and return only the class name.

extension UIViewController {
    var className: String {
        NSStringFromClass(self.classForCoder).components(separatedBy: ".").last!
    }
}

Solution 5:[5]

Swift 4

Suppose we have class with name HomeViewController. Then you can get name of class with the following code:

let class_name = "\(HomeViewController.classForCoder())"

The classForCoder() method returns AnyClass object (name of your class) which we convert to string for user.

Solution 6:[6]

Here is a swift3 version of isuru's answer.

extension UIViewController {
    var className: String {
        return NSStringFromClass(self.classForCoder).components(separatedBy: ".").last!;
    }
}

Solution 7:[7]

Swift 5 solution:

extension NSObject {
  var className: String {
    return String(describing: type(of: self))
  }

  class var className: String {
    return String(describing: self)
  }
}

USAGE:

class TextFieldCell: UITableVIewCell {
}

class LoginViewController: UIViewController {
  let cellClassName = TextFieldCell.className
}

Solution 8:[8]

The property is called dynamicType in Swift.

Solution 9:[9]

Use String.init(describing: self.classForCoder)

example:

let viewControllerName = String.init(describing: self.classForCoder)
print("ViewController Name: \(viewControllerName)")

Solution 10:[10]

How about:

    extension NSObject {

    static var stringFromType: String? {
        return NSStringFromClass(self).components(separatedBy: ".").last
    }

    var stringFromInstance: String? {
        return NSStringFromClass(type(of: self)).components(separatedBy: ".").last
    }
}

Solution 11:[11]

We can also do: String(describing: Self.self) in Swift 5.1.