tags:

views:

1606

answers:

1

I've tried using both:

NSClassFromString and objc_getclass

to return a class, so I can create it at runtime, but both functions return nil for some classes, for example "TestClass". Note that NSClassFromString works for me for 99% of classes.

If I add

[TestClass class];

before I call NSStringFromClass or objc_getclass it, it works. and if I try to just create the class using a class reference, i.e.:

[TestClass alloc];

it works too. So how can I force the class to load at runtime so NSClassFromString or objc_getclass will not return nil?

+1  A: 

The Objective-C Runtime Reference can help you here. For example, the documentation for objc_getClass says, "The Class object for the named class, or nil if the class is not registered with the Objective-C runtime." Looking around for discussion of registration you find this tidbit in objc_getClassList: "The Objective-C runtime library automatically registers all the classes defined in your source code. You can create class definitions at runtime and register them with the objc_addClass function." (And of course the docs are out of date because objc_addClass is deprecated, objc_allocateClassPair and objc_registerClassPair in its place.)

This looks like a world of hurt if you don't make this easy on yourself. Any chance you can just reference your dynamic classes in code when the app starts?

Neil Mix
that may be what I have to do.... seems strange though!
Brad Parks