Created
March 19, 2014 17:37
-
-
Save paulrehkugler/9647085 to your computer and use it in GitHub Desktop.
Default Protocol Method Implementation in Objective-C
This file contains 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
// 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for this paul lol