- @protocol MyProtocol
-
- - (void) doSomething;
-
- @end
-
- @interface MyClass : NSObject<MyProtocol>
- {
- }
-
- @end
-
- @implementation MyClass
-
- - (void) doSomething {
- }
-
- @end
-
- @interface MyOtherClass : MyClass
- {
-
- }
-
- @end
-
- @implementation MyOtherClass
-
- - (void) doSomething {
- }
-
- @end
-
-
- int main (int argc, const char * argv[])
- {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
-
- MyClass *obj_one = [MyClass new];
- BOOL one_conforms = [obj_one conformsToProtocol:@protocol(MyProtocol)];
-
- MyOtherClass *obj_two = [MyOtherClass new];
-
-
-
- BOOL two_conforms = [obj_two conformsToProtocol:@protocol(MyProtocol)];
- NSLog(@"obj_one conformsToProtocol: %d", one_conforms);
- NSLog(@"obj_two conformsToProtocol: %d", two_conforms);
- [pool drain]; return 0;
- }
-
-
- obj_one conformsToProtocol: 1
- obj_two conformsToProtocol: 1
-
-
- MyOtherClass *obj_two = [MyOtherClass new];
-
- BOOL conforms_two = class_conformsToProtocol([obj_two class], @protocol(MyProtocol));
- NSLog(@"obj_two conformsToProtocol: %d", conforms_two);
-
-
- obj_two conformsToProtocol: 0
|
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步