Qadir Hussain
Qadir Hussain

Reputation: 8856

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

Upvotes: 60

Views: 60800

Answers (11)

Pranav Kasetti
Pranav Kasetti

Reputation: 9915

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

Upvotes: 3

DEEPAK KUMAR
DEEPAK KUMAR

Reputation: 361

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
}

Upvotes: 6

Isuru
Isuru

Reputation: 31283

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

Upvotes: 20

SmileBot
SmileBot

Reputation: 19642

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

Upvotes: 1

prabhu
prabhu

Reputation: 1206

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)

Upvotes: 44

Naresh
Naresh

Reputation: 955

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.

Upvotes: 9

Zahidur Rahman Faisal
Zahidur Rahman Faisal

Reputation: 727

Use String.init(describing: self.classForCoder)

example:

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

Upvotes: 2

cnotethegr8
cnotethegr8

Reputation: 7510

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

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

Upvotes: 68

zingle-dingle
zingle-dingle

Reputation: 1701

Here is a swift3 version of isuru's answer.

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

Upvotes: 7

juangdelvalle
juangdelvalle

Reputation: 838

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

var className = NSStringFromClass(yourClass.classForCoder)

Upvotes: 80

fluidsonic
fluidsonic

Reputation: 4676

The property is called dynamicType in Swift.

Upvotes: 1

Related Questions