简体   繁体   English

快速获取 UIViewController 的类名

[英]Get class name of UIViewController in swift

How do you get the class name of a UIViewController class in Swift?如何在 Swift 中获取UIViewController类的类名?

In Objective-C, we can do something like this:在 Objective-C 中,我们可以这样做:

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 ie last screen没有UIViewControllerclass方法,即最后一个屏幕

要知道您的班级名称,您可以这样称呼:

var className = NSStringFromClass(yourClass.classForCoder)

不需要知道您的班级名称的最干净的方法是这样的。

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

A simple way in swift 3 is to write the below code: swift 3 中的一个简单方法是编写以下代码:

for instances:为实例:

let className = String(describing: self)

for classes:对于课程:

let className = String(describing: YourViewController.self)

Expanding on juangdelvalle's answer .扩展 juangdelvalle 的答案

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:同样在某些情况下,Swift 中的NSStringFromClass返回格式如下的字符串:

< project name >.viewControllerClassName . <项目名称>.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!
    }
}

Swift 4斯威夫特 4

Suppose we have class with name HomeViewController .假设我们有一个名为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. classForCoder()方法返回AnyClass对象(您的类的名称),我们将其转换为用户的字符串。

Here is a swift3 version of isuru's answer.这是 isuru 答案的 swift3 版本。

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

Swift 5 solution:斯威夫特 5 解决方案:

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
}

该属性在 Swift 中称为dynamicType

Use String.init(describing: self.classForCoder)使用String.init(describing: self.classForCoder)

example:例子:

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

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

我们也可以这样做: Swift 5.1 中的String(describing: Self.self)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM