我有一个使用枚举的特殊模式,它始终是 String 类型,并且枚举(不是它的原始值)也遵守特定的协议(protocol)。正常写,是这样的……
private enum IndicatorA : String, IndicatorProtocol
{
case X
case Y
case Z
}
我想做的是找出是否可以做任何事情来隐含字符串,所以我只需要输入这个...
private enum IndicatorA : Indicator
{
case X
case Y
case Z
}
...其中 Indicator
指示原始值是类型 String
并且枚举本身也遵守 IndicatorProtocol
协议(protocol)。
我已经尝试了这两种方法来想出一些可以用作原始值类型的东西,但都无法编译。另外,从技术上讲,将协议(protocol)放在字符串上,而不是枚举上,所以这不是我所追求的。 (同样,我想要枚举上的协议(protocol),而不是原始值。)
class Indicator : String, IndicatorProtocol
{
}
struct Indicator : String, IndicatorProtocol
{
}
那么有什么方法可以实现我在 Swift 2.x 或 3.x 中想要实现的目标吗?也许以某种方式对协议(protocol)进行限制?
最佳答案
对此有几点看法:
假设可以使枚举包含一个 Indicator
作为其原始值,重要的是要注意这实际上不会使枚举本身符合 IndicatorProtocol
,只有它的 rawValue 会符合。换句话说:
enum IndicatorA : String, IndicatorProtocol
与以下内容不同:
enum IndicatorA : StringThatConformsToIndicatorProtocol
对于第一个示例,您可以像这样访问协议(protocol):
let example:IndicatorA = IndicatorA(rawValue:"Test")
example.someProtocolMethod()
对于第二个示例,您必须像这样访问协议(protocol):
let example:IndicatorA = IndicatorA(rawValue:"Test")
example.rawValue.someProtocolMethod()
因为在第一个示例中,枚举本身符合协议(protocol),但在第二个示例中,枚举的原始值符合协议(protocol)。
综上所述,您可以通过创建符合 StringLiteralConvertible
、 的
和您自己的 Indicator
结构来使第二个示例正常工作EquatableIndicatorProtocol
。示例:
protocol IndicatorProtocol {
func printHello()
}
struct Indicator: IndicatorProtocol, StringLiteralConvertible, Equatable {
let string: String
init(name value: String) {
self.string = value
}
init(stringLiteral value: String) {
self.string = value
}
init(extendedGraphemeClusterLiteral value: String) {
self.string = value
}
init(unicodeScalarLiteral value: String) {
self.string = value
}
func printHello() {
print("Hello from \(string)")
}
}
func ==(left:Indicator, right:Indicator) -> Bool {
return left.string == right.string
}
enum IndicatorA : Indicator {
case One = "One", Two = "Two"
}
let test = IndicatorA.One
print(test.rawValue.printHello()) // prints "Hello from One
但要达到我认为您一开始就希望得到的结果,需要做一些额外的工作!
关于swift - 您能否将类型和协议(protocol)结合起来,并将其用作枚举的基本类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39130290/