Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Complete example/test program for quick and dirty function to determine if a method in an Objective-C protocol is required or optional. Example for my answer to this stackoverflow question:
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@protocol TestProtocol <NSObject>
- (void)methodA;
- (void)methodB;
@interface TestProtocolClass : NSObject <TestProtocol> @end
@implementation TestProtocolClass
- (void)methodA
NSLog(@"%s", __PRETTY_FUNCTION__);
BOOL MethodInProtocolIsRequired(Protocol *protocol, SEL methodSelector)
struct objc_method_description methodDesc = protocol_getMethodDescription(protocol, methodSelector, YES, YES);
return != NULL;
int main(int argc, const char * argv[])
@autoreleasepool {
Protocol *p = objc_getProtocol("TestProtocol");
BOOL methodAIsRequired = MethodInProtocolIsRequired(p, @selector(methodA));
BOOL methodBIsRequired = MethodInProtocolIsRequired(p, @selector(methodB));
NSLog(@"methodA is required? %i methodB is required? %i", methodAIsRequired, methodBIsRequired);
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment