Get class name of UIViewController in swift

后端 未结 9 828
野的像风
野的像风 2020-12-25 11:20

How to get class name of UIViewController Class in swift

In Objective C, we can do something like this :

self.appDelegate = (shAppDeleg         


        
相关标签:
9条回答
  • 2020-12-25 11:38

    Here is a swift3 version of isuru's answer.

    extension UIViewController {
        var className: String {
            return NSStringFromClass(self.classForCoder).components(separatedBy: ".").last!;
        }
    }
    
    0 讨论(0)
  • 2020-12-25 11:44

    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.

    0 讨论(0)
  • 2020-12-25 11:49

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

    var className = NSStringFromClass(yourClass.classForCoder)
    
    0 讨论(0)
提交回复
热议问题