Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do you conform to a protocol in Objective-C?

Objective-C Language Protocols Conforming to Protocols It is also possible for a class to conform to multiple protocols, by separating them with comma. Like when conforming to a single protocol, the class must implement each required method of each protocols, and each optional method you choose to implement.

What is a protocol in Objective-C?

Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. This chapter describes the syntax to define a formal protocol, and explains how to mark a class interface as conforming to a protocol, which means that the class must implement the required methods.


According to the docs,

[MyClass conformsToProtocol:@protocol(MyProtocol)];

should work.


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

Class<MyProtocol> someClassPointer = nil;

you can use:

[someClassPointer.class conformsToProtocol:@protocol(MyProtocol)];