简体   繁体   中英

How do I get class from NSString?

for example, I have

NSString *myClass = @"RootViewController"

How do I get class from NSString?

试试这个

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

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

Class class = NSClassFromString(@"RootViewController");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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