protocol extension for Objective-C like Swift
定义protocol
@protocol PETest <NSObject>
@optional
- (id)test:(id)arg1 arg2:(id)arg2 arg3:(id)arg3;
@end
定义protocol 默认行为
@extension(PETest)
- (id)test:(id)arg1 arg2:(id)arg2 arg3:(id)arg3 {
NSLog(@"===================>%@===%@=====%@",arg1,arg2,arg3);
[self testLog];
return [NSObject new];
}
- (void)testLog {
NSLog(@"======================class: %@",self.class);
}
@end
具体使用
@interface PEProtocolTest : NSObject<PETest>
@end
@implementation PEProtocolTest
@end
...
[[PEProtocolTest new] test:@"xxxxxxxxxxxxx" arg2:@"yyyyyyyy" arg3:@"zzzzzzzzzzzz"];
结果
===================>xxxxxxxxxxxxx===yyyyyyyy=====zzzzzzzzzzzz
======================class: PEProtocolTest
pod "ProtocolExtension"
carl shen
ProtocolExtension is available under the MIT license. See the LICENSE file for more info.