How do I get list of all classes conform to certain protocol in Xcode 4?

大城市里の小女人 提交于 2020-01-01 08:29:04

问题


I am new to Xcode. I want to know how can I know all classes conform to certain protocol in Xcode 4.3.1? And how can I know all subclasses of one class?


回答1:


Use the Objective-C runtime functions.

  • objc_getClassList to get the list of Classes
  • class_getSuperclass or the -superclass method to walk the superclass chain
  • class_conformsToProtocol or the -conformsToProtocol: method to check if a class conforms to a protocol



回答2:


Protocol *protocol = @protocol(YourProtocol);

int numberOfClasses = objc_getClassList(NULL, 0);
Class *classList = malloc(numberOfClasses * sizeof(Class));
numberOfClasses = objc_getClassList(classList, numberOfClasses);

for (int idx = 0; idx < numberOfClasses; idx++) 
{
    Class class = classList[idx];
    if (class_getClassMethod(class, @selector(conformsToProtocol:)) && [class conformsToProtocol:protocol])
    {
        NSLog(@"%@", NSStringFromClass(class));
    }
}
free(classList);


来源:https://stackoverflow.com/questions/9882984/how-do-i-get-list-of-all-classes-conform-to-certain-protocol-in-xcode-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!