隐藏类符合协议的逻辑 [英] Hide logic that class conforms to protocol

查看:37
本文介绍了隐藏类符合协议的逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以隐藏该类符合某种协议吗?就像在 Objective-C 中一样 - 只是用来在 .m 文件中添加协议,其他类(来自其他文件)没有看到它.

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.

例如.我有一个带有文本字段的测试单元格.我想隐藏,这个单元格符合协议.类似的东西:

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?

推荐答案

此功能已被 Swift 团队声明为不太可能"实现.这是关于它的原始线程:https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160229/011666.html

This capability has been stated by the Swift team as "unlikely" to be implemented. Here is the original thread about it: https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160229/011666.html

关于这个特定场景的具体声明是:

The specific statement about this particular scenario was:

私有一致性

现在,协议一致性的可见性不亚于符合类型的访问和协议的访问中的最小值.因此,符合公共协议的公共类型必须提供公开的一致性.人们可以想象取消那个限制,以便可以引入私有一致性:

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())

在什么情况下应该打印P"?如果定义了 foo()在与 X 到 P 的一致性相同的模块中?如果电话是定义在同一个模块中作为 X 到 P 的一致性?绝不?前两个答案中的任何一个都需要在考虑模块的动态铸造基础设施发生了特定的动态转换(第一个选项)或在哪里形成了存在主义(第二个选项),而第三个答案打破静态和动态类型系统之间的联系——没有一个这是一个可以接受的结果.

Under what circumstances should it print "P"? If foo() is defined within the same module as the conformance of X to P? If the call is defined within the same module as the conformance of X to 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.

这篇关于隐藏类符合协议的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆