如何制定描述字符串可表示枚举的协议?
How to make protocol describing string-representable enums?
我有一个方法可以调用某个管理器的方法来使用某个键保存 int 值。我的方法接收 int 和一些 EnumKey 枚举值作为键,提取 EnumKey 的 rawValue 并将其作为字符串传递给管理器:
set(value: Int, forKey key: EnumKey) {
SomeManager.saveIntValueWithStringKey(valueToSave: value, keyToSave: key.rawValue)
}
enum EnumKey: String {
case One="first key"
case Two="second key"
}
我想通过允许我的方法接收每个带有字符串原始值而不是 EnumKey 的枚举来使其更通用。在方法的实现中,我将密钥参数的类型从 EnumKey 替换为 GenericKey 协议,并使 EnumKey 符合此协议:
set(value: Int, forKey key: GenericKey) {
SomeManager.saveIntValueWithStringKey(valueToSave: value, keyToSave: key.rawValue)
}
protocol GenericKey {
var rawValue: String { get }
}
enum EnumKey: String, GenericKey {
case One="first key"
case Two="second key"
}
但是这个String, GenericKey
看起来有点丑。除了 RawRepresentable 和 String 原始类型之外,我希望每个可表示字符串的枚举都能自动适应,而无需提及它符合 GenericKey 协议。类似于:
protocol GenericKey: RawRepresentable {
associatedtype RawValue = String
}
但是编译器说 "Protocol can be used only as a generic constraint because it has Self or associated type requirements".
有什么简单的方法可以解释编译器协议仅描述具有 String 类型的 RawValue 的 RawRepresentable 事物?
您可以将函数定义为泛型,并将泛型类型定义为 RawRepresentable
和 RawValue
类型 String
,如下所示:
class Test {
func set<T: RawRepresentable>(value: Int, forKey key: T) where T.RawValue == String {
print("value \(value), key: \(key.rawValue)")
}
}
enum EnumKey: String {
case One="first key"
case Two="second key"
}
let t = Test()
t.set(value: 3, forKey: EnumKey.One) // prints "value 3, key: first key"
我有一个方法可以调用某个管理器的方法来使用某个键保存 int 值。我的方法接收 int 和一些 EnumKey 枚举值作为键,提取 EnumKey 的 rawValue 并将其作为字符串传递给管理器:
set(value: Int, forKey key: EnumKey) {
SomeManager.saveIntValueWithStringKey(valueToSave: value, keyToSave: key.rawValue)
}
enum EnumKey: String {
case One="first key"
case Two="second key"
}
我想通过允许我的方法接收每个带有字符串原始值而不是 EnumKey 的枚举来使其更通用。在方法的实现中,我将密钥参数的类型从 EnumKey 替换为 GenericKey 协议,并使 EnumKey 符合此协议:
set(value: Int, forKey key: GenericKey) {
SomeManager.saveIntValueWithStringKey(valueToSave: value, keyToSave: key.rawValue)
}
protocol GenericKey {
var rawValue: String { get }
}
enum EnumKey: String, GenericKey {
case One="first key"
case Two="second key"
}
但是这个String, GenericKey
看起来有点丑。除了 RawRepresentable 和 String 原始类型之外,我希望每个可表示字符串的枚举都能自动适应,而无需提及它符合 GenericKey 协议。类似于:
protocol GenericKey: RawRepresentable {
associatedtype RawValue = String
}
但是编译器说 "Protocol can be used only as a generic constraint because it has Self or associated type requirements".
有什么简单的方法可以解释编译器协议仅描述具有 String 类型的 RawValue 的 RawRepresentable 事物?
您可以将函数定义为泛型,并将泛型类型定义为 RawRepresentable
和 RawValue
类型 String
,如下所示:
class Test {
func set<T: RawRepresentable>(value: Int, forKey key: T) where T.RawValue == String {
print("value \(value), key: \(key.rawValue)")
}
}
enum EnumKey: String {
case One="first key"
case Two="second key"
}
let t = Test()
t.set(value: 3, forKey: EnumKey.One) // prints "value 3, key: first key"