Can I get a notification when a new class is added to the runtime? (Goal C)

The Objective-C runtime allows you to dynamically create and register pairs of classes using objc_allocateClassPairand, objc_registerClassPairrespectively. Can I get a notification when a new class is added to the runtime? (I'm only interested in registered Objective-C classes, if that helps, unregistered classes will be a bonus.)

If it is not possible to receive a notice, what is the next most effective? Most iOS devices have severe computational limitations, so my options are limited. I am open to hacking at runtime, if that allows. I understand that I could just plug in +load, but this will only work with NSObject subclasses, not pure Swift classes.

"Pure Swift classes? But pure Swift classes are not Objective-C classes!" you say? Think again. Try exploring a pure Swift class using the runtime, and you will see that there is more to it than meets the eye! "How can I add a dynamic Swift class?" Well, I can just load the dynamic structure that uses Swift!

Since @SpaceDog asked, this is for the developer utility I'm working on. For further clarification: I do not know when objc_allocateClassPairor will be called objc_registerClassPair, since my project is a library.

+4
source share
1 answer

Short answer: None.

Long answer: view.

There are two ways you can do this. Unfortunately, both of them require blind flow and interrogation.

Synchronous callback is not possible if you do not rewrite characters.
(of course, if this is an option, you have a number of other solutions)

  • The first option, which uses only public APIs: you can follow the number objc_getClassListfor change at any point.

    , , DYLD . , . , .

    . objc_getClassList ( -), (, O(N) /), , .


  1. , , - (ab) API- gdb_objc_realized_classes.

    NXMapTable (- -, objective-c) (const char *), (aka. objc_class).

    . NXMapTable OSX (10.4?), maptable.h Apple Open Source.

    :

    #import "maptable.h"
    
    extern NXMapTable *gdb_objc_realized_classes;
    
    int main() {
        printf("Realized classes: %d\n", NXCountMapTable(gdb_objc_realized_classes));
    
        Class superClass = [NSObject class];
        Class myKls = objc_allocateClassPair(superClass, "TestClass", 0);
        objc_registerClassPair(myKls);
    
        printf("Realized classes: %d\n", NXCountMapTable(gdb_objc_realized_classes));
    }
    

    : gdb_objc_realized_classes zero , , , , .


, , .

, ( , , , Foundation Foundation 1500 ), , , , objc_getClassList , , , .

, , - . , / .

.

+3

All Articles