2

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]);

Binarian
  • 12,296
  • 8
  • 53
  • 84

3 Answers3

10

I believe the function you're looking for is:

NSClassFromString(@"AnyClass");

which lets you do:

id obj = [[NSClassFromString(@"AnyClass") alloc] init];
Alnitak
  • 334,560
  • 70
  • 407
  • 495
3

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

0

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.

Richard Stelling
  • 25,607
  • 27
  • 108
  • 188
  • No i read the class name from a file and i want to instantiate it at runtime. –  Apr 15 '09 at 14:19