简体   繁体   English

如何快速确定一类符合哪些协议?

[英]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.

相关问题 在 Swift 中,如何声明符合一个或多个协议的特定类型的变量? - In Swift, how can I declare a variable of a specific type that conforms to one or more protocols? 如果我的课程符合具有相同属性的两个协议,将会怎样? - What will happen if my class conforms to two protocols having same property? 我的班级符合两个协议 - 如何为每个协议设置不同的委托? - My class conforms to two protocols - how can I set different delegates to each of them? 如何在Swift中使用协议公开Objective-C对象的私有类方法? - How do I expose a private class method of an Objective-C object using protocols in Swift? 符合协议和类的Swift属性 - Swift Property that conforms to a Protocol and Class 如何将方法的返回类型编写为符合协议的 class + swift - How to write a return type of a method as a class that conforms to a protocol + swift 为什么 Swift Class-Only 协议需要 AnyObject 继承? - Why do Swift Class-Only Protocols need AnyObject Inheritance? Swift检查类是否符合协议始终为true - Swift check if class conforms to protocol always true Swift类/结构隐式符合NSObjectProtocol? - Swift class/struct implicitly conforms to NSObjectProtocol? Swift 4+实例化符合协议的类变量 - Swift 4+ instantiating a class variable that conforms to a protocol
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM