Create objective-c class instance by name?

Mark picture Mark · Jul 23, 2009 · Viewed 48.2k times · Source

Is it possible to create an instance of a class by name? Something like:

NSString* className = @"Car";
id* p = [Magic createClassByName:className];
[p turnOnEngine];

I don't know if this is possible in objective-c but seems like it would be,

Answer

Chris McCall picture Chris McCall · Jul 23, 2009
id object = [[NSClassFromString(@"NameofClass") alloc] init];