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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:37:42  来源:igfitidea点击:

Get a view controller's class name as string

iosobjective-ccocoa-touchintrospection

提问by xonegirlz

If I have a UIViewControllersubclass, how can I get the class name as string? I've tried doing [vc class], but this returns a Classobject, 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];