Is there a way to #import a class from a string in Objective-C at run time? Any methods that would produce a similar result would also be welcome.
Edit:
I want access to a class whose name I determine at runtime. So something like this:
NSString *className = getClassName();
Class myClass = loadClass(className);
myClass *myVar = [[myClass alloc] init];
Is there any way to do this without putting a static #import directive for myClass at the top of the file?
The
#importdirective does not “import” a class — it inserts the text from the named file into the current file. This obviously isn’t useful at runtime after the source has been compiled.What you want is to create a bundle with the classes and dynamically load the bundle. In order to be able to talk to the classes from the core program, you’ll probably want to have some common protocol that bundled classes implement.