I want to convert a string to a class name. Imagine that I have a string, which changes, containing a class name, for example, the string "MyExampleClass". Now, I want to create an object of the class MyExampleClass. I have to get the class name from the string. I want to do something like the following. (Consider the code just as a sketch.)
NSString *classNameStr = "MyExampleClass";
id theClass = [UIClass classFromString:classNameStr];
theClass *myObject = [[theClass alloc] init];
What is the right way to do this?
Here’s what you’d want:
Note that you can’t use
theClassas a type name (i.e.theClass *myObject). You’ll have to useidfor that.