简体   繁体   English

如何在Objective-C中引用协议?

[英]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)? 我知道协议的指令是@protocol,就像@selector一样,但引用协议的“类型”是什么(例如@Selector的SEL)? In MacOSX stack, it's Protocol *? 在MacOSX堆栈中,它的协议*?

You reference it as: 您将其引用为:

id<TheNameOfTheProtocol> aVariableToThatProtocol;

Or if a message wants a (Protocol *) object: 或者,如果消息需要(Protocol *)对象:

[myObject conformsToProtocol:@protocol(TheNameOfTheProtocol)];

id <YourProtocol> delegate (which is used to reference the protocol)? id <YourProtocol> delegate (用于引用协议)?

I referred to the apple's official DOC , and found an simple example to refer to other protocols in a protocol: 我提到了苹果公司的官方DOC ,并找到了一个简单的例子来引用协议中的其他协议:

#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: 协议B的声明方式如下:

#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. 请注意,以这种方式使用@protocol指令只是通知编译器B是稍后定义的协议。 It doesn't import the interface file where protocol B is defined. 它不会导入定义协议B的接口文件。


And here're more things you'd like to know about protocol : 以下是您想要了解的有关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. 它们都声明了方法,并且在运行时它们都是由Class实例的对象类和协议实例的协议表示的。 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. 源代码可以使用@protocol()指令引用协议对象 - 声明协议的相同指令,除了它在这里有一组尾随括号。 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(). 与类名称不同,协议名称不指定对象 - 除了@protocol()内部。


And what's more, the protocol is possible to check whether an object conforms to a protocol by sending it a conformsToProtocol: message: 而且, protocol可以通过向对象发送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 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. conformsToProtocol: test类似于respondsToSelector:测试单个方法,除了它测试协议是否已被采用(并且可能是它声明已实现的所有方法),而不仅仅是一个特定方法是否已实现。 Because it checks for all the methods in the protocol, conformsToProtocol: can be more efficient than respondsToSelector: . 因为它检查协议中的所有方法,所以conformsToProtocol:respontsToSelector更有效

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. 所述conformsToProtocol:测试也是像isKindOfClass:测试,不同之处在于它测试用于基于协议,而不是基于继承层次一个类型的类型。

It's the same as on OS X: 它与OS X上的相同:

Protocol * p = objc_getProtocol("UITableViewDataSource");

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

typedef struct objc_object Protocol;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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