58

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

shim
  • 9,289
  • 12
  • 69
  • 108
Qadir Hussain
  • 8,721
  • 13
  • 89
  • 124

11 Answers11

76

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

var className = NSStringFromClass(yourClass.classForCoder)
juangdelvalle
  • 798
  • 6
  • 8
62

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

let name = String(describing: type(of: self))
cnotethegr8
  • 7,342
  • 8
  • 68
  • 104
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)
prabhu
  • 1,158
  • 1
  • 12
  • 27
  • Note e.g. `String(describing: UIViewController())` will output `` for example, whereas `String(describing: type(of: UIViewController()))` will just output `UIViewController` – shim Apr 21 '22 at 03:41
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!
    }
}
Michal Šrůtek
  • 1,647
  • 16
  • 17
Isuru
  • 30,617
  • 60
  • 187
  • 303
  • when you say "in some cases" do you mean it doesn't return a consistent answer? – Craig.Pearce Oct 22 '16 at 03:03
  • 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. – Dunbar Nov 25 '19 at 16:26
  • why not simply use? ``` let viewControllerName = "\(viewControllerInstance.classForCoder())" ``` – Noor Feb 11 '21 at 11:21
8

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.

koen
  • 5,383
  • 7
  • 50
  • 89
Naresh
  • 869
  • 8
  • 17
7

Here is a swift3 version of isuru's answer.

extension UIViewController {
    var className: String {
        return NSStringFromClass(self.classForCoder).components(separatedBy: ".").last!;
    }
}
zingle-dingle
  • 1,681
  • 16
  • 16
5

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
}
DEEPAK KUMAR
  • 341
  • 4
  • 8
2

Use String.init(describing: self.classForCoder)

example:

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

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

Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71
1

The property is called dynamicType in Swift.

fluidsonic
  • 4,655
  • 2
  • 24
  • 34
  • Note: this property was deprecated a while ago and is fully phased out in the latest Swift at least – shim Apr 21 '22 at 03:44
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
    }
}
SmileBot
  • 19,393
  • 7
  • 65
  • 62