-
Notifications
You must be signed in to change notification settings - Fork 1
MDProtocolImplementation
剑川道长 edited this page Dec 6, 2018
·
10 revisions
MDProtocolImplementation是通过runtime实现对协议方法的默认实现
代码示例请参考Demo中测试用例:
MDToolsDemoTests/MDProtocolImplementation/TMDProtocolImplementation.m
MDToolsDemoTests/MDProtocolImplementation/TMDProtocolImplementation_protocol.h
MDToolsDemoTests/MDProtocolImplementation/TMDProtocolImplementation_protocol.m
// TMDProtocolImplementation_protocol.h
@protocol TMDProtocolImplementation_protocol <NSObject>
@optional
- (NSString *)testMethod;
@end
// TMDProtocolImplementation_protocol.m
@implementationProtocol(TMDProtocolImplementation_protocol)
- (NSString *)testMethod {
return [self.class description];
}
@end
使用@implementationProtocol(TMDProtocolImplementation_protocol)代替@implementation TMDProtocolImplementation_protocol。
实现协议中的方法,如果其他遵守TMDProtocolImplementation_protocol协议的类为实现该协议方法,会默认执行此处的实现。
// TMDProtocolImplementation.m
@implementation TMDProtocolImplementation
__ImplementationProtocol__
//...
- (void)testProtocolImplementation {
NSLog(@"%@", [self testMethod]);
}
//...