Objective-C 协议
我在名为 TileOverlay.h 的文件中遇到了以下代码:
@protocol TileOverlay <MKOverlay>
我试图明确地理解它在做什么。然后,其他几个覆盖层会导入此 .h 文件。该文件本质上只是创建 MKOverlay 类的修改版本吗?
如果没有,您能解释一下它的作用吗?
I came across the following bit of code in a file called TileOverlay.h:
@protocol TileOverlay <MKOverlay>
I'm trying to understand explicitly what this is doing. Several other overlays then import this .h file. Does this file essentially just create a modified version of the MKOverlay class?
If not, can you please clarify what it does?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这称为协议继承。
MKOverlay
是一个协议,定义了一组供对象实现的方法。TileOverlay
继承了MKOverlay
,这意味着符合TileOverlay
的对象应该实现MKOverlay
中的方法和MKOverlay
中的方法。 code>TileOverlay这里是一个链接这更多地讨论了协议继承
It's called protocol inheritance.
MKOverlay
is a protocol, defining a set of methods for objects to implement.TileOverlay
inherits forMKOverlay
, meaning that an object conforming toTileOverlay
, should implement methods fromMKOverlay
and methods fromTileOverlay
Here is a link that talks a little bit more about protocol inheritance
Objective-C 中的协议就是 Java 或 C# 中的接口。这是任何其他类都可以履行的契约,用 Obj-C 的说法符合。
这:
定义一个名为
TileOverlay
的协议,该协议本身扩展了MKOverlay
。也就是说,通过遵守TileOverlay
,您还必须遵守MKOverlay
。Obj-C 中的协议就像 Jave 或 C# 中的接口一样,与实现无关。它没有做任何事情。它只是编译时的一个标记,如果需要的话,可以在运行时检查某些功能是否存在。
A protocol in Objective-C is what you in Java or C# would call an interface. It is a contract that any other class can fulfill, in Obj-C parlance conform to.
This:
Defines a protocol named
TileOverlay
that in itself extendsMKOverlay
. That is by conforming toTileOverlay
you must also conform toMKOverlay
.Protocols in Obj-C, just as interfaces in Jave or C#, is not related to implementation. It does not do anything. It's just a marker at compile time, and at run-time if you want to, to check if some functionally exist.
MKOverlay
是一个协议,TileOverlay
是一个扩展MKOverlay
的协议。任何符合
TileOverlay
协议的类也都符合MKOverlay
协议。请参阅 协议中的协议在文档中有详细的解释。
MKOverlay
is a protocol, andTileOverlay
is a protocol that extendsMKOverlay
.Any class that conforms to the
TileOverlay
protocol also conforms to theMKOverlay
protocol.Refer to Protocols Within Protocols in the documentation for a detailed explanation.
您可以将协议想象为 Java 或 C# 中的接口,基本上它们声明了实现类必须遵循的契约。不同之处在于,在 Objective C 中,您可以将某些声明的方法设为可选。
You can think of protocols like interfaces in Java or C#, basically they declare a contract that the implementing classes must follow. The difference is that in Objective C you can make some of the declared methods optional.