Objective-C:从超类获取子类列表 [英] Objective-C: Get list of subclasses from superclass

查看:109
本文介绍了Objective-C:从超类获取子类列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中,有一种方法可以询问类是否有任何子类实现.

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.

修改:

我有一组可以处理某些类型数据的类.每个处理器子类都有一个基类,该基类提供了每个处理器所需的方法.

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.

我还从2009年开始发现了这个问题(我在发布此消息之前进行了搜索,但未找到任何内容)

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.

编辑2 :

+ (void)load方法似乎是我要寻找的完美解决方案.所以我现在有以下内容:

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.

我的下一个问题是(可能是一个愚蠢的问题),如何存储在registerSubclass方法中注册的类.有没有一种方法可以让我以后阅读的类变量?

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?

编辑3 :

在此处找到了一些示例代码一个简单,可扩展的可可中的HTTP服务器

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.

推荐答案

您永远不能列出一个类的子类.使用(几乎)任何编程语言.这是面向对象编程的基本属性之一.

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

考虑更改对象模型.

您可能想要创建一个抽象类和不同的子类,但是您不应该从抽象类访问子类.您应该创建另一个对象(Factory类),该对象注册子类并在需要时选择合适的对象.

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. 您的工厂类必须知道所有子类的名称(在编译时或在读取某些配置文件时).
  2. 您的工厂类有一个方法,任何人都可以将要注册的类的名称传递给该方法.如果您希望外部库注册新的子类,那么这是正确的解决方案.然后,您可以将子类注册代码放入库的主标头中.

这篇关于Objective-C:从超类获取子类列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆