简体   繁体   English

类符合协议的隐藏逻辑

[英]Hide logic that class conforms to protocol

Are there any ways to hide that class conforms to some protocol? 有什么方法可以隐藏该类符合某种协议? Like in Objective-C - just used to add Protocol in .m file and other classes (from another files) didn't see it. 就像在Objective-C中一样 -仅用于在.m文件中添加协议,而其他类(来自另一个文件)则看不到它。

For example. 例如。 I have a test cell which has a textfield. 我有一个具有文本字段的测试单元。 I want to hide, that this cell conforms to protocol. 我想隐藏一下,这个单元格符合协议。 Something like that: 像这样:

class TestCell: UITableViewCell {

}

fileprivate extension TestCell : UITextFieldDelegate {

}

But compiler swears me. 但是编译器向我发誓。 Any elegant solution? 有什么优雅的解决方案吗?

This capability has been stated by the Swift team as "unlikely" to be implemented. Swift团队已将此功能声明为“不太可能”实现。 Here is the original thread about it: https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160229/011666.html 这是关于它的原始主题: https : //lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160229/011666.html

The specific statement about this particular scenario was: 关于此特定方案的具体说明是:

Private conformances 私人合规

Right now, a protocol conformance can be no less visible than the minimum of the conforming type's access and the protocol's access. 现在,协议一致性在可见性方面至少要低于一致性类型访问和协议访问的最小值。 Therefore, a public type conforming to a public protocol must provide the conformance publicly. 因此,符合公共协议的公共类型必须公开提供一致性。 One could imagine removing that restriction, so that one could introduce a private conformance: 可以想象删除该限制,以便可以引入一种私有一致性:

 public protocol P { } public struct X { } extension X : internal P { … } // X conforms to P, but only within this module 

The main problem with private conformances is the interaction with dynamic casting. 私有一致性的主要问题是与动态转换的交互。 If I have this code: 如果我有此代码:

 func foo(value: Any) { if let x = value as? P { print(“P”) } } foo(X()) 

Under what circumstances should it print “P”? 在什么情况下应打印“ P”? If foo() is defined within the same module as the conformance of X to P? 是否在与X到P的一致性相同的模块中定义了foo()? If the call is defined within the same module as the conformance of X to P? 是否在与X到P的一致性相同的模块中定义了该调用? Never? 决不? Either of the first two answers requires significant complications in the dynamic casting infrastructure to take into account the module in which a particular dynamic cast occurred (the first option) or where an existential was formed (the second option), while the third answer breaks the link between the static and dynamic type systems—none of which is an acceptable result. 前两个答案中的任何一个都需要在动态转换基础结构中带来显着的复杂性,以考虑发生特定动态转换的模块(第一个选项)或形成存在的模块(第二个选项),而第三个答案则打破了静态类型系统和动态类型系统之间的链接-没有一个可以接受的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM