您的位置:首页 > 其它

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

2013-01-31 14:34 417 查看
使用-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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: