ios 快速获取 UIViewController 的类名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26358422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get class name of UIViewController in swift
提问by Qadir Hussain
How to get class name of UIViewController
Class in 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 like this
但很快我就这样试过了
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let last_screen = appDelegate.popScreens?.lastObject as UIViewController
cant do this.
不能这样做。
if last_screen.class != self.navigationController.visibleViewController.class{
//....
}
no class method of UIViewController
i.e last screen
没有UIViewController
ie 最后一屏的类方法
回答by juangdelvalle
To know your class name you can call something like this:
要知道您的班级名称,您可以这样调用:
var className = NSStringFromClass(yourClass.classForCoder)
回答by cnotethegr8
The cleanest way without needing to know the name of your class is like this.
不需要知道你的班级名称的最干净的方法是这样的。
let name = String(describing: type(of: self))
回答by prabhu
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)
回答by Isuru
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:
我将它添加为扩展,以便它可以重用并且更容易从任何视图控制器调用。同样在某些情况下NSStringFromClass
,Swift 会以如下格式返回一个字符串:
< 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 {
return NSStringFromClass(self.classForCoder).componentsSeparatedByString(".").last!
}
}
回答by Naresh
Swift 4
斯威夫特 4
Suppose we have class with name HomeViewController
. Then you can get name of class with the following code:
假设我们有 name 的类HomeViewController
。然后您可以使用以下代码获取类名:
let class_name = "\(HomeViewController.classForCoder())"
The classForCoder()
method returns AnyClass
object (name of your class) which we convert to string for user.
该classForCoder()
方法返回AnyClass
对象(您的类的名称),我们将其转换为用户的字符串。
回答by zingle-dingle
Here is a swift3 version of isuru's answer.
这是 isuru 答案的 swift3 版本。
extension UIViewController {
var className: String {
return NSStringFromClass(self.classForCoder).components(separatedBy: ".").last!;
}
}
回答by smileBot
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
}
}
回答by Zahidur Rahman Faisal
Use String.init(describing: self.classForCoder)
用 String.init(describing: self.classForCoder)
example:
例子:
let viewControllerName = String.init(describing: self.classForCoder)
print("ViewController Name: \(viewControllerName)")
回答by fluidsonic
The property is called dynamicType
in Swift.
该属性dynamicType
在 Swift 中被调用。