Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6748421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:33:46+00:00 2026-05-26T12:33:46+00:00

In Objective-C is there a way to ask a Class if there are any

  • 0

In Objective-C is there a way to ask a Class if there are any Subclass implementations.

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.

Edit:

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.

Each class knows what data it can process and some classes can process certain types of data better than others.

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.

The factory would then make the decision on which class to instantiate based on which class says it can process the data the best.

I have also found this question from 2009 (I did search before I posted this but didn’t find anything) Discover subclasses of a given class in Obj-C.

Edit 2:

The + (void)load method looks to be the perfect solution to what I am looking for. So I now have the following:

+ (void)registerSubclass:(Class)subclass {
    NSLog(@"Registered %@", subclass);
}

In my base class the this is my subs.

+(void)load {
    [BaseSwitch registerSubclass:[self class]];
}

This now displays a debug message for each of the subclasses.

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?

Edit 3:

Found some example code here A simple, extensible HTTP server in Cocoa

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.

@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 < [registeredSubclasses count]; i++) {
        NSLog(@"%@", [registeredSubclasses objectAtIndex:i]);
    }
}

@end

Thanks for everyones suggestions, I will leave the question unanswered for a couple more days incase something else comes up.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:33:47+00:00Added an answer on May 26, 2026 at 12:33 pm

    You can never list subclasses of a class. In (almost) any programming language. This is one of the basic properties of Object Oriented Programming.

    Consider changing your object model.

    What you probably want is to create an abstract class and different subclasses but you shouldn’t access the subclasses from the abstract class. You should create another object (Factory class) which registers the subclasses and selects the appropiate one when needed.

    Note that you cannot efficiently register a class from the class itself. For a class code to be executed, the class has to be loaded first. That means, you have to import its header in some other class and that means that you are actually registering the class by importing its header.
    There are two possible solutions:

    1. Your factory class has to know the names of all subclasses (either at compile time or reading some configuration file).
    2. Your factory class has a method to which anyone can pass the name of a class to be registered. This is the right solution if you want external libraries to register a new subclass. Then you can put the subclass registration code into the main header of the library.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a somewhat messily-formatted Objective-C code base. Is there a way to have
Is there any way to have a Shortcut Recorder in Objective-C, where the user
Is there a way in objective-c/Cocoa to alloc an object when the class name
Is there any way to create a property like this C# property in Objective-C?
I'm a beginner with Objective C and iPhone development. It's there any way to
is there any way to get the users screen resolution with objective c? Thanks.
Is there any way to call c++ functions from objective-c code without .mm extension?
I'm starting objective-c development and I would like to ask the best way to
Is there any way to navigate the structure of an Objective-C file by alpha
Is there a way in Objective-C to set a property value of a class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.