ios 以字符串形式获取视图控制器的类名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10889543/
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 a view controller's class name as string
提问by xonegirlz
If I have a UIViewController
subclass, how can I get the class name as string? I've tried doing [vc class]
, but this returns a Class
object, not an NSString
.
如果我有一个UIViewController
子类,如何将类名作为字符串获取?我试过这样做[vc class]
,但这会返回一个Class
对象,而不是一个NSString
.
回答by jscs
NSStringFromClass
Returns the name of a class as a string.
NSString * NSStringFromClass ( Class aClass );
NSStringFromClass
以字符串形式返回类的名称。
NSString * NSStringFromClass ( Class aClass );
回答by Hardik Darji
You can do something like this
你可以做这样的事情
NSString *strClass = NSStringFromClass([viewController class]);
回答by GoZoner
Use the function:
使用函数:
const char * class_getName(Class cls)
as
作为
class_getName ([vc class]);
回答by shebelaw
you can do some thing like the following when you instantiate your object:
实例化对象时,您可以执行以下操作:
[[NSClassFromString(@"className1") alloc] init];