How do I get class from NSString?

…衆ロ難τιáo~ 提交于 2019-12-07 04:44:22

问题


for example, I have

NSString *myClass = @"RootViewController"

How do I get class from NSString?


回答1:


Try this

 id obj= [[NSClassFromString(@"RootViewController") alloc] init];



回答2:


You can use

Class class = NSClassFromString(@"RootViewController");

NSClassFromString

Obtains a class by name.

Class NSClassFromString ( NSString *aClassName );

Parameters

aClassName

The name of a class.

Return Value

The class object named by aClassName, or nil if no class by that name is currently loaded. If aClassName is nil, returns nil. Availability

Available in iOS 2.0 and later.

There is a lot of methods like this in foundation framework

NSGetSizeAndAlignment
NSClassFromString
NSStringFromClass
NSSelectorFromString
NSStringFromSelector
NSStringFromProtocol
NSProtocolFromString

Refer Foundation Functions Reference




回答3:


Class class = NSClassFromString(@"RootViewController");


来源:https://stackoverflow.com/questions/12535860/how-do-i-get-class-from-nsstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!