title: Objective-C: Get list of subclasses from superclass
body: <p>In Objective-C is there a way to ask a Class if there are any Subclass implementations.</p>
  
  <p>I have a Base class which has multiple subclasses. I would like to loop through all the subclasses and perform a class selector on each of them.</p>
  
  <p><strong>Edit</strong>:</p>
  
  <p>I have a set of classes that can process certain types of data. Each of the processors subclass a base class that provides methods that each processor needs.</p>
  
  <p>Each class knows what data it can process and some classes can process certain types of data better than others.</p>
  
  <p>I would like to have a class method on each class that would provide a response back to a factory class that says yes i can process that data, and give a indication of how well it can process it.</p>
  
  <p>The factory would then make the decision on which class to instantiate based on which class says it can process the data the best.</p>
  
  <p>I have also found this question from 2009 (I did search before I posted this but didn't find anything) <a href="https://stackoverflow.com/questions/1810628/discover-subclasses-of-a-given-class-in-obj-c">Discover subclasses of a given class in Obj-C</a>.</p>
  
  <p><strong>Edit 2</strong>:</p>
  
  <p>The <code>+ (void)load</code> method looks to be the perfect solution to what I am looking for. So I now have the following:</p>
  
  <pre><code>+ (void)registerSubclass:(Class)subclass {
      NSLog(@"Registered %@", subclass);
  }
  </code></pre>
  
  <p>In my base class the this is my subs.</p>
  
  <pre><code>+(void)load {
      [BaseSwitch registerSubclass:[self class]];
  }
  </code></pre>
  
  <p>This now displays a debug message for each of the subclasses.</p>
  
  <p>My next question is (probably a stupid one), how do I store the classes that get registered in the registerSubclass method. Is there a way to have class variable that I can read later?</p>
  
  <p><strong>Edit 3</strong>:</p>
  
  <p>Found some example code here <a href="http://cocoawithlove.com/2009/07/simple-extensible-http-server-in-cocoa.html" rel="nofollow noreferrer">A simple, extensible HTTP server in Cocoa</a></p>
  
  <p>Which has left me with the following, seems pretty simple after all is said and done. But I thought I would put it here for future reference.</p>
  
  <pre><code>@implementation BaseSwitch
  
  static NSMutableArray *registeredSubclasses;
  
  + (void)registerSubclass:(Class)subclass {
      if (registeredSubclasses == nil) {
          registeredSubclasses = [[NSMutableArray alloc] init];
      }
  
      [registeredSubclasses addObject:subclass];
  
      NSLog(@"Registered %@", subclass);
  }
  
  + (void)logSubclasses {
      for (int i = 0; i &lt; [registeredSubclasses count]; i++) {
          NSLog(@"%@", [registeredSubclasses objectAtIndex:i]);
      }
  }
  
  @end
  </code></pre>
  
  <p>Thanks for everyones suggestions, I will leave the question unanswered for a couple more days incase something else comes up.</p>