Protocol* x = @protocol(aProtocolName) 在目标 c 中做了啥?

Posted

技术标签:

【中文标题】Protocol* x = @protocol(aProtocolName) 在目标 c 中做了啥?【英文标题】:what does Protocol* x = @protocol(aProtocolName) do in objective c?Protocol* x = @protocol(aProtocolName) 在目标 c 中做了什么? 【发布时间】:2011-11-17 10:42:09 【问题描述】:

我发现我可以在 objc 中做这样的事情:

Protocol* aProtocol = @protocol(NSObject);

但是这种技术有什么用呢?

【问题讨论】:

执行此操作:***.com/questions/3436257/… 【参考方案1】:

例如,您可以拥有一个容器,该容器提供一项服务来检查其所有内容是否符合协议。

Protocol *contentProtocol;

- (void)setContentProtocol:(Protocol *)proto;

那么容器的用户可以调用:

[container setContentProtocol:@protocol(MyProtocol)];

并且可以在容器中的某个地方进行检查:

[[item class] conformsToProtocol:contentProtocol];

【讨论】:

或者只是[item conformsToProtocol:contentProtocol]【参考方案2】:

what is use of Formal Protocol Object 您可以使用它来检查对象是否符合协议。

[anotherObject conformsToProtocol:aProtocol];

【讨论】:

以上是关于Protocol* x = @protocol(aProtocolName) 在目标 c 中做了啥?的主要内容,如果未能解决你的问题,请参考以下文章

objective c, protocol

在asyncio.Protocol.data_received中调用协同程序

ISO 基础之 (十三) protocol 协议

Protocol buffers--python 实践 protocol buffers vs json

[HMLY]12.iOS中的Protocol

ios category,protocol理解