Objective-C Runtime: best way to check if class conforms to protocol?

后端 未结 2 1546
故里飘歌
故里飘歌 2020-12-07 11:54

I have a Class (but no instance) and need to know if it conforms to a certain protocol. However, Class can be subclassed several times and class_conformsToProtocol() ignores

相关标签:
2条回答
  • 2020-12-07 12:23

    Or, in case it is a general pointer, like:

    Class<MyProtocol> someClassPointer = nil;
    

    you can use:

    [someClassPointer.class conformsToProtocol:@protocol(MyProtocol)];
    
    0 讨论(0)
  • 2020-12-07 12:35

    According to the docs,

    [MyClass conformsToProtocol:@protocol(MyProtocol)];
    

    should work.

    0 讨论(0)
提交回复
热议问题