Created
March 24, 2014 11:23
-
-
Save Tricertops/9738484 to your computer and use it in GitHub Desktop.
Runtime @protocol introspection
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
| + (void)enumerateAdoptedProtocolsOfProtocol:(Protocol *)protocol usingBlock:(void(^)(Protocol *adoptedProtocol))block { | |
| unsigned int adoptedCount = 0; | |
| Protocol * __unsafe_unretained *adoptedProtocolList = protocol_copyProtocolList(protocol, &adoptedCount); | |
| if (adoptedProtocolList) { | |
| for (NSUInteger index = 0; index < adoptedCount; index++) { | |
| Protocol *adoptedProtocol = adoptedProtocolList[index]; | |
| block(adoptedProtocol); | |
| } | |
| free(adoptedProtocolList); | |
| } | |
| } | |
| + (void)enumerateMethodsInProtocol:(Protocol *)protocol | |
| required:(BOOL)onlyRequired | |
| instance:(BOOL)onlyInstance | |
| usingBlock:(void(^)(SEL selector, NSMethodSignature *signature))block { | |
| unsigned int methodCount = 0; | |
| struct objc_method_description *methods = protocol_copyMethodDescriptionList(protocol, onlyRequired, onlyInstance, &methodCount); | |
| if (methods) { | |
| for (NSUInteger index = 0; index < methodCount; index++) { | |
| struct objc_method_description method = methods[index]; | |
| NSMethodSignature *signature = [NSMethodSignature signatureWithObjCTypes:method.types]; | |
| block(method.name, signature); | |
| } | |
| free(methods); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment