Create a class at runtime - Why do NSClassFromString AND objc_getclass return nil?

后端 未结 3 1096
南笙
南笙 2021-01-01 06:57

I\'ve tried using both:

NSClassFromString and objc_getclass 

to return a class, so I can create it at runtime, but both functions return ni

相关标签:
3条回答
  • 2021-01-01 07:34

    I had to manually add the class (e.g. TestClass.m) to the build settings. Build Phases > Compile Sources > + > TestClass.m

    0 讨论(0)
  • 2021-01-01 07:38

    I ran into this same problem. In my case I had code which was looking up the class using

    Class aClass = objc_getClass("Foo");
    

    Which worked when the Foo class was in the same project bundle as my AppDelegate.

    As part of refactoring the code, I moved the Foo class and associated model classes out of the AppDelegate project into a common static lib project that would be easier to test and reuse.

    When I moved the Foo class into libFooBar the objc_getClass("Foo") now returned nil instead of the Foo class.

    To solve this, I put in call to the class I am interested in so that the classes from libFooBar are registered with the Objective-C runtime. I did this like so:

    Foo* foo = [[Foo alloc] init];
    [foo release];
    Class aClass = objc_getClass("Foo");
    

    Now the objc_getClass("Foo") returns the Foo class again instead of nil.

    I can only assume that the static library classes are not registered until a call to one of the classes in the static library is made.

    0 讨论(0)
  • 2021-01-01 07:38

    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?

    0 讨论(0)
提交回复
热议问题