如何判断一个类是否实现了某个protocol

来源:互联网 发布:淘宝店主真人实拍福利 编辑:程序博客网 时间:2024/05/17 08:00
使用-conformsToProtocol:函数,具体用法如下:
@protocol MyProtocol <NSObject>@required- (void)onUpdate;@end@interface ProtocolTest : NSObject<MyProtocol>- (void)onUpdate;@end@interface ProtocolTestChildClass : ProtocolTest@end
调用:
ProtocolTestChildClass *protocolChild = [[ProtocolTestChildClass alloc] init];BOOL hasProtocol = [protocolChild conformsToProtocol:@protocol(MyProtocol)];CCLOG(@"has protocol = %d", hasProtocol);
结果:
has protocol = 1