Get class name of UIViewController in swift

后端 未结 9 913
野的像风
野的像风 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:29

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

提交回复
热议问题