MacOS finding classes at launchtime
<p>Is there a way to determine which classes are present at launch time?</p>

<p>I have a Swift OS-X app that offers an API to add functionality. Not dynamically though, everything is fixed at launch time.</p>

<p>But is it possible to find out which classes are present at launch, and then from there to call a certain (inherited) static operation on these classes?</p>

<p>Absent this possibility I will have to create an initialisation routine that must be updated every time a new subclass is added. I would like to avoid this.</p>

<p>Clarification: I have a protocol call it MyProtocol. At launch of the App I would like to call an operation from MyProtocol on all classes that implement MyProtocol.</p>

<p>After examining the runtime manual I have gotten as far as:</p>

<pre><code>#import &lt;Foundation/Foundation.h&gt;
#include "Test.h"
#import &lt;objc/objc-class.h&gt;

void activateLaunchActions() {

    // Get a list of all classes
    int numClasses = 0, newNumClasses = objc_getClassList(NULL, 0);
    Class *classes = NULL;
    while (numClasses &lt; newNumClasses) {
        numClasses = newNumClasses;
        Class newClasses[numClasses];
        classes = newClasses;
        newNumClasses = objc_getClassList(classes, numClasses);
    }

    // Get the protocol they have to confirm to
    Protocol *prot = objc_getProtocol("MyProtocol");

    // Get the selector to be called
    SEL sel = sel_registerName("launchAction");

    // Create the launchAction caller from objc_msgSend
    typedef void (*send_type)(Class, SEL);
    send_type callLauchAction = (send_type)objc_msgSend;

    // Call the registration for all classes that confirm to the protocol
    for (int i=0; i&lt;numClasses; i++) {
        if (class_conformsToProtocol(classes[i], prot)) {
            callLauchAction(classes[i], sel);
        }
    }
}
</code></pre>

<p>The "prot" is always NULL. Hence the test later fails and the launchAction is never called.</p>

<p>I have read that protocol objects are not always present if they are not used by a class, however this protocol is used by several classes.</p>

<p>Any suggestions as how to fix this would be welcome.</p>