Voloda2
Voloda2

Reputation: 12617

How do I get class from NSString?

for example, I have

NSString *myClass = @"RootViewController"

How do I get class from NSString?

Upvotes: 5

Views: 5317

Answers (3)

Midhun MP
Midhun MP

Reputation: 107231

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

Upvotes: 7

brynbodayle
brynbodayle

Reputation: 6626

Class class = NSClassFromString(@"RootViewController");

Upvotes: 5

NeverBe
NeverBe

Reputation: 5038

Try this

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

Upvotes: 16

Related Questions