So I have a class MyClass
. It's name is part of char * myclasses[]
. I'd like to allocate it using the objc runtime api.
1) Is this the best way to do this? My goal is to not allocate 10 or 15 ViewControllers, until they actually need to be used, and also not have a giant switch statement to create them.
Class aClass = NSClassFromString ( [NSString stringWithUTF8String:myclasses[1]] );
id myClass = class_createInstance(aClass, 0);
I'm trying to understand registering the class also..
2) Since I'm not creating a new class (just allocating) at runtime, I shouldn't have to register it, correct?
3) How does class registration normally work? (feel free to point me to a document somewhere).
[ [ <class> alloc ] init ]
. If you mean you don't want to load the classes until you need to alloc an instance of those classes--that's going to be tricky. You will somehow need to avoid referring to the class symbolically anywhere in your code, and even then I don't think you can avoid loading every class in your app under iOS. (iOS doesn't allow dynamic code loading)