简体   繁体   中英

How to reference Protocol in Objective-C?

I know the directive for a protocol is @protocol just like @selector, but what is the "type" for referencing a Protocol (eg. SEL for @Selector)? In MacOSX stack, it's Protocol *?

You reference it as:

id<TheNameOfTheProtocol> aVariableToThatProtocol;

Or if a message wants a (Protocol *) object:

[myObject conformsToProtocol:@protocol(TheNameOfTheProtocol)];

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

I referred to the apple's official DOC , and found an simple example to refer to other protocols in a protocol:

#import "B.h"

@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 like this:

#import "A.h"

@protocol B
  - bar:(id <A>)anObject;
@end

Note that using the @protocol directive in this manner simply informs the compiler that B is a protocol to be defined later. It doesn't import the interface file where protocol B is defined.


And here're more things you'd like to know about protocol :

In many ways, protocols are similar to class definitions. They both declare methods, and at runtime they're both represented by objects—classes by instances of Class and protocols by instances of Protocol. Like class objects, protocol objects are created automatically from the definitions and declarations found in source code and are used by the runtime system. They're not allocated and initialized in program source code.

Source code can refer to a protocol object using the @protocol() directive —the same directive that declares a protocol, except that here it has a set of trailing parentheses. The parentheses enclose the protocol name:

  Protocol *myXMLSupportProtocol = @protocol(MyXMLSupport); 

This is the only way that source code can conjure up a protocol object. Unlike a class name, a protocol name doesn't designate the object—except inside @protocol().


And what's more, the protocol is possible to check whether an object conforms to a protocol by sending it a conformsToProtocol: message:

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 conformsToProtocol: test is like the respondsToSelector: test for a single method, except that it tests whether a protocol has been adopted (and presumably all the methods it declares implemented) rather than just whether one particular method has been implemented. Because it checks for all the methods in the protocol, conformsToProtocol: can be more efficient than respondsToSelector: .

The conformsToProtocol: test is also like the isKindOfClass: test, except that it tests for a type based on a protocol rather than a type based on the inheritance hierarchy.

It's the same as on OS X:

Protocol * p = objc_getProtocol("UITableViewDataSource");

It's declared in <objc/runtime.h> :

typedef struct objc_object Protocol;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM