【问题标题】: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);

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

【问题讨论】:

标签: objective-c


【解决方案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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    相关资源
    最近更新 更多