Get class name of UIViewController in swift

IosObjective CUiviewcontrollerSwift

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

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

var className = NSStringFromClass(yourClass.classForCoder)

Solution 2 - Ios

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

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

Solution 3 - Ios

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

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

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

Here is a swift3 version of isuru's answer.

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

Solution 7 - Ios

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

The property is called dynamicType in Swift.

Solution 9 - Ios

Use String.init(describing: self.classForCoder)

example:

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

Solution 10 - Ios

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionQadir HussainView Question on Stackoverflow
Solution 1 - IosjuangdelvalleView Answer on Stackoverflow
Solution 2 - Ioscnotethegr8View Answer on Stackoverflow
Solution 3 - IosprabhuView Answer on Stackoverflow
Solution 4 - IosIsuruView Answer on Stackoverflow
Solution 5 - IosNareshView Answer on Stackoverflow
Solution 6 - Ioszingle-dingleView Answer on Stackoverflow
Solution 7 - IosDEEPAK KUMARView Answer on Stackoverflow
Solution 8 - IosfluidsonicView Answer on Stackoverflow
Solution 9 - IosZahidur Rahman FaisalView Answer on Stackoverflow
Solution 10 - IossmileBotView Answer on Stackoverflow