61

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

11 Answers 11

81

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

var className = NSStringFromClass(yourClass.classForCoder)
Sign up to request clarification or add additional context in comments.

1 Comment

It returns <project_name>.<class_name> but in most cases users need the second part only
68

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

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

Comments

44

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)

1 Comment

Note e.g. String(describing: UIViewController()) will output <UIViewController: 0x141d0f7e0> for example, whereas String(describing: type(of: UIViewController())) will just output UIViewController
20

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!
    }
}

3 Comments

when you say "in some cases" do you mean it doesn't return a consistent answer?
Actually the first part before the dot is the module name. So if your class is defined in an external library you will see the module name. The last part however should be the real class name. That is unless Apple decide to change this function for some reason.
why not simply use? ``` let viewControllerName = "(viewControllerInstance.classForCoder())" ```
9

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.

Comments

7

Here is a swift3 version of isuru's answer.

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

2 Comments

There's an extra ';' in the end.
:) @alasker . guess I'm not a swift developer at heart.
6

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
}

Comments

3

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

Comments

2

Use String.init(describing: self.classForCoder)

example:

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

Comments

1

The property is called dynamicType in Swift.

1 Comment

Note: this property was deprecated a while ago and is fully phased out in the latest Swift at least
1

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
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.