Created
January 20, 2012 21:28
-
-
Save mysteriouspants/1649705 to your computer and use it in GitHub Desktop.
All Classes Conforming to Protocol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
NSArray* allClassesConformingToProtocol(Protocol* p); // fwd dec; makes compiler stfu |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "allClassesConformingToProtocol.h" | |
#include <objc/runtime.h> | |
NSArray* allClassesConformingToProtocol(Protocol* p) | |
{ | |
NSMutableArray* arr = [NSMutableArray array]; | |
int numClasses = objc_getClassList(NULL, 0); | |
Class* allClasses=(Class*)malloc(sizeof(Class*)*numClasses); | |
objc_getClassList(allClasses, numClasses); | |
for (size_t i=0; | |
i<numClasses; | |
++i) | |
if (class_conformsToProtocol(allClasses[i], p)) | |
[arr addObject:allClasses[i]]; | |
free(allClasses); | |
return arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment