【问题标题】:Descendant Enumeration in Objective-CObjective-C 中的后代枚举
【发布时间】:2010-09-26 21:24:21
【问题描述】:

是否有可能在objective-c中得到一个特定类的所有后代类的列表?

类似:

  @interface A : NSObject
  @end

  @interface B : A
  @end

  @interface C : A
  @end

  NSArray *descendants = [A allDescendants];  // descendants = [B, C]

【问题讨论】:

    标签: objective-c inheritance runtime metaprogramming


    【解决方案1】:

    我能想到的唯一方法是枚举运行时中的整个类列表(使用objc_getClassList 获得)并测试每个类的isKindOfClass:A

    这可能是唯一的解决方案,因为类不维护与其后代的链接(仅与其超类)。

    【讨论】:

      【解决方案2】:

      你能以某种方式使用类成员吗?调用[[A class] superclass] 会为self 的超类返回Class 类的实例。我认为你可以从它的Class 中获取一个类的名称 - 这会做你想要的吗?

      这根本不能真正处理协议,但也许这至少会为您指明正确的方向。

      【讨论】:

        【解决方案3】:

        例如,我通过Ken Ferry 链接到solutionWil Shipley 博客文章。

        基本上是在上课。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多