Protocol extension for Objective-C
Your protocol:
@protocol Forkable <NSObject>
@optional
- (void)fork;
@required
- (NSString *)github;
@endProtocol extension, add default implementation, use @defs magic keyword
@defs(Forkable)
- (void)fork {
NSLog(@"Forkable protocol extension: I'm forking (%@).", self.github);
}
- (NSString *)github {
return @"This is a required method, concrete class must override me.";
}
@endYour concrete class
@interface Forkingdog : NSObject <Forkable>
@end
@implementation Forkingdog
- (NSString *)github {
return @"https://github.com/forkingdog";
}
@endRun test
[[Forkingdog new] fork];Result
[Console] Forkable protocol extension: I'm forking (https://github.com/forkingdog).