将协议作为方法参数传递

发布于 2024-12-05 21:20:10 字数 279 浏览 1 评论 0原文

首先让我解释一下我的意思。我不想为协议输入参数:

-(void)someMethod:(id<SomeProtocol>)someArgument;

我真正想要的是将协议传递给方法,就像我可以将类传递给方法一样(以下内容是不正确的,但它希望解释我的意思)想做):

-(void)someMethod:(Protocol)someArgument;

然后我希望能够使用协议来检查一组对象是否实现它。

First let me explain what I don't mean. I don't want to type an argument to a protocol:

-(void)someMethod:(id<SomeProtocol>)someArgument;

What I do want to is to pass a protocol to a method in the same way I can pass a Class to a method (The following is incorrect, but it hopefully explains what I want to do):

-(void)someMethod:(Protocol)someArgument;

I would then like to be able to use the Protocol to check whether a set of objects implement it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉风有信 2024-12-12 21:20:10

如果您在编码时知道协议的名称,请使用 @protocol(SomeProtocol) 获取指向该协议的指针,类似于使用 @selector(x)< /代码>。

除此之外,您只需使用类标识符 Protocol 引用协议 - 因此您的方法声明将如下所示:

-(void)someMethod:(Protocol*)someArgument

您可以在文档中查看 NSObjectconfesToProtocol 的示例:

<一href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/clm/NSObject/conformsToProtocol ">http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/clm/NSObject/conformsToProtocol :

If you know the name of a protocol at coding-time, use @protocol(SomeProtocol) to get a pointer to that protocol, similar to how you'd use @selector(x).

Beyond that, you just refer to protocols with the class identifier Protocol -- so you're method declaration would look like:

-(void)someMethod:(Protocol*)someArgument

You can see an example in the docs for NSObject conformsToProtocol:

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/clm/NSObject/conformsToProtocol:

巡山小妖精 2024-12-12 21:20:10

协议是一个类,因此您只需像任何其他对象类型一样编写 - (void)someMethod:(Protocol *)someArgument 。您可以在 conformsToProtocol: 的声明中看到这一点:

+ (BOOL)conformsToProtocol:(Protocol *)aProtocol

Protocol is a class, so you just write - (void)someMethod:(Protocol *)someArgument like with any other object type. You can see this in the declaration for conformsToProtocol::

+ (BOOL)conformsToProtocol:(Protocol *)aProtocol
预谋 2024-12-12 21:20:10
- (void) executeRequest:(id<Protocol1>)request andCompletion:(id<Protocol2>)response

将协议传递给参数的唯一方法

因为id<..>意味着在传递抛出参数之前它需要符合该协议

- (void) executeRequest:(id<Protocol1>)request andCompletion:(id<Protocol2>)response

the only way to pass protocol into an argument

because id<..> means it needs to conform to that protocol before pass throw the argument

╰◇生如夏花灿烂 2024-12-12 21:20:10

我不建议使用协议。它将掩盖您的代码实际依赖的接口。使用id*。这实际上就是可可框架传递协议的方式。请原谅我使用的文字,如果我不这样做,它认为我正在尝试做 HTML。

I don't recommend using protocol. It will obscure which interface your code actually relies on. Use id<yourprotocol>*. This is actually how the cocoa frameworks pass protocols. Forgive the use of words if I don't it thinks I'm trying to do HTML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文