I am trying to create an instance of a type based on another type passed into a method. I find that NSClassFromString
works just fine if used with a string literal, as in
id instance = [[NSClassFromString(@"TheNameOfTheClassIWant") alloc] init];
but if I construct the string with something like
NSString *inClassName = [[protoInstance class] description];
NSString *outClassName = [inClassName stringByAppendingFormat:@"IWant"];
id instance = [[NSClassFromString(outClassName) alloc] init];
that instance
is nil
. Does NSClassFromString
only work with literals? Is something happening at compile time to make NSClassFromString
work?