Skip to content

Instantly share code, notes, and snippets.

@paulrehkugler
Created March 19, 2014 17:37
Show Gist options
  • Save paulrehkugler/9647085 to your computer and use it in GitHub Desktop.
Save paulrehkugler/9647085 to your computer and use it in GitHub Desktop.
Default Protocol Method Implementation in Objective-C
// SomeProtocol.h
@protocol SomeProtocol <NSObject>
- (void) protocolMethod;
@end
// NSObject+SomeProtocolDefaultImplementation.h
@interface NSObject(SomeProtocolDefaultImplementation)
- (void) protocolMethod;
@end
// NSObject+SomeProtocolDefaultImplementation.m
@implementation NSObject(SomeProtocolDefaultImplementation)
- (void) protocolMethod
{
if (![self conformsToProtocol:@protocol(SomeProtocol)])
{
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"You must conform to protocol SomeProtocol to access this method.", NSStringFromSelector(_cmd)] userInfo:nil];
}
// method implementation
}
@worldofnick
Copy link

thanks for this paul lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment