[英]How do I determine what protocols a class conforms to in swift?
In objective-c, you can always check a class's protocols through its public headers. 在Objective-c中,您始终可以通过其公共标头检查类的协议。 But if you have a class in swift, how do you know what protocols it conforms to? 但是,如果您很快就上了一堂课,您怎么知道它遵循什么协议? Is this even possible? 这有可能吗?
(Yes, I realize Apple's documentation lists the protocols it conforms to, but that doesn't seem like a real solution, since you might be working with a private framework or have some other theoretical reason for wanting to know an object's protocols.) (是的,我知道Apple的文档列出了它所遵循的协议,但这似乎不是真正的解决方案,因为您可能正在使用私有框架,或者有其他一些理论上的原因想要了解对象的协议。)
Also, yes, I know you can check for a specific protocol using the technique outlined here: https://stackoverflow.com/a/37351027/18961 另外,是的,我知道您可以使用此处概述的技术检查特定的协议: https : //stackoverflow.com/a/37351027/18961
In Xcode, you can command-click on any Swift type to get a header-like view of its public interface, including protocol conformances added in both the original definition and in extensions. 在Xcode中,您可以命令单击任何Swift类型以获取其公共接口的类似于标题的视图,包括在原始定义和扩展中添加的协议一致性。
This doesn't show protocol conformances added by libraries outside the defining library, eg if you command-click String
you won't see your own extension String: MyFunkyProtocol
. 这不会显示由定义库外部的库添加的协议一致性,例如,如果您单击“ String
您将看不到自己的extension String: MyFunkyProtocol
。 But I think it is essentially the header-file-like thing you're looking for. 但我认为这实际上是您要查找的类似于头文件的内容。
For the Swift standard lib, http://swiftdoc.org/ is a nice reference. 对于Swift标准库, http://swiftdoc.org/是一个不错的参考。
I don't believe you can do it without leaning on the Objective-C runtime. 我不相信您可以不依靠Objective-C运行时来做到这一点。 Import ObjectiveC
and use the class_copyProtocolList()
function. 导入ObjectiveC
并使用class_copyProtocolList()
函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.