How to reference a protocol in Objective-C?

I know that the directive for the protocol is @protocol, like @selector, but what is a “type” for a protocol reference (for example, SEL for @Selector)? On the MacOSX stack, is this a protocol *?

+6
source share
3 answers

You refer to it as:

id<TheNameOfTheProtocol> aVariableToThatProtocol; 

Or if the message wants an object (Protocol *) :

 [myObject conformsToProtocol:@protocol(TheNameOfTheProtocol)]; 
+13
source

id <YourProtocol> delegate (which is used to refer to the protocol)?

I mentioned an Apple DOC spokesperson and found a simple example to reference other protocols in the protocol:

 #import "Bh" @protocol B; // To break the recursive cycle, you must use the @protocol directive to make a forward reference to the needed protocol instead of importing the interface file where the protocol is defined @protocol A - foo:(id <B>)anObject; @end 

where protocol B is declared as follows:

 #import "Ah" @protocol B - bar:(id <A>)anObject; @end 

Note that using the @protocol directive this way simply tells the compiler that B is a protocol that will be defined later. It does not import the interface file where protocol B is defined.


And here are a few more things you would like to learn about protocol :

In many ways, protocols are similar to class definitions. They declare methods, and at run time they are represented as class objects by class instances and protocols by protocol instances. Like class objects, protocol objects are created automatically from the definitions and declarations found in the source code and used by the runtime system. They are not allocated or initialized in the program source code.

The source code can refer to the protocol object using the @protocol () directive - the same directive that declares the protocol, except that it has a set of end brackets here. The brackets enclose the protocol name:

  Protocol *myXMLSupportProtocol = @protocol(MyXMLSupport); 

This is the only way that the source code can call the protocol object. Unlike the class name, the protocol name does not mean an object, except inside @protocol ().


And what else, protocol can be checked to see if the object conforms to the protocol by sending it a message conformsToProtocol: ::

 if ( ! [receiver conformsToProtocol:@protocol(MyXMLSupport)] ) { // Object does not conform to MyXMLSupport protocol // If you are expecting receiver to implement methods declared in the // MyXMLSupport protocol, this is probably an error } 

The correspondence of ToProtocol: test is similar to responsesToSelector: test for a single method, except that it checks if the protocol has been accepted (and, presumably, all the methods that it declares), and not just whether one particular method has been implemented. Since it checks all the methods in the protocol, ToProtocol matches: it may be more efficient than responding to a request:

Matching ToProtocol: test is also similar to the isKindOfClass: test test, except that it tests a type based on the protocol and not a type based on the inheritance hierarchy.

+4
source

This is the same as on OS X:

 Protocol * p = objc_getProtocol("UITableViewDataSource"); 

It is declared in <objc/runtime.h> :

 typedef struct objc_object Protocol; 
+2
source

Source: https://habr.com/ru/post/905328/


All Articles