views:

135

answers:

3

Hi there,

I need to get the class object out of a string, containing the name of the class at runtime. I found a function called objc_getClass, but I'm not sure if it's really what I search.

Can I implement it like this? NSString *name = @"AnyClass"; Class *myClass = objc_getClass([name UTF8String]);

A: 

Are you sure you don't mean:

AnyClass *myClassInstance = [[AnyClass alloc] init];

Or has this class been instantiated and you need a pointer to it? You might need to give more details.

rjstelling
No i read the class name from a file and i want to instantiate it at runtime.
+6  A: 

I believe the function you're looking for is:

NSClassFromString(@"AnyClass");

which lets you do:

id obj = [[NSClassFromString(@"AnyClass") alloc] init];
Alnitak
like I wrote already, but thank you
we overlapped answers :)
Alnitak
+2  A: 

I found the answer myself ^^NSClassFromString is my friend :)