我正在尝试实现类似于 Swift 如何在实现 Codable
的类中定义的枚举上使用 CodableKeys
协议(protocol)集的方式。在我的例子中,类是 CommandHandler
,枚举是 CommandIds
,它不需要编译器的代码生成,因为枚举总是被显式指定。
这是我所追求的简化版本...
protocol CommandId{}
protocol CommandHandler{
associatedtype CommandIds : CommandId, RawRepresentable
}
class HandlerA : CommandHandler{
enum CommandIds : String, CommandId{
case commandA1
case commandA2
}
}
class HandlerB : CommandHandler{
enum CommandIds : String, CommandId{
case commandB1
case commandB2
case commandB3
}
}
func processHandler<T:CommandHandler>(_ handler:T){
// Logic to iterate over CommandIds. <-- This is where I get stumped
}
let handlerA = HandlerA()
processHandler(handlerA)
我在这里为 processHandler
中的代码苦苦挣扎,因为我不确定如何从处理程序实例中获取枚举值。
那我错过了什么?获取关联枚举值的代码是什么?
最佳答案
好吧,我相信我已经准备好所有的部分来展示你如何在 Swift 中做到这一点。事实证明,我修改后的问题恰好处于正确的边缘。
这是我用 Swift 4 编写的示例...
首先,您可以通过以下方式定义实现此功能所需的协议(protocol)。从设计的角度来看,它们分别与 CodableKeys 和 Codable 同义。
protocol CommandId : EnumerableEnum, RawRepresentable {}
protocol CommandHandler{
associatedtype CommandIds : CommandId
}
这里有一个协议(protocol)及其相关扩展,使枚举的“大小写”值可枚举。您只需让您的枚举遵守 EnumerableEnum
协议(protocol),您就会得到一个“值”数组。
由于上面的 CommandId
协议(protocol)已经应用于相关枚举,我们通过在其自己的定义中也应用 EnumerableEnum
协议(protocol)来简化事情。这样我们只需要将 CommandId
应用到我们的枚举中,我们就可以同时获得两者。
public protocol EnumerableEnum : Hashable {
static var values: [Self] { get }
}
public extension EnumerableEnum {
public static var values: [Self] {
let valuesSequence = AnySequence { () -> AnyIterator<Self> in
var caseIndex = 0
return AnyIterator {
let currentCase: Self = withUnsafePointer(to: &caseIndex){
$0.withMemoryRebound(to: self, capacity: 1){
$0.pointee
}
}
guard currentCase.hashValue == caseIndex else {
return nil
}
caseIndex += 1
return currentCase
}
}
return Array(valuesSequence)
}
}
这是实现我的 CommandHandler/CommandId 协议(protocol)的两个类
class HandlerA : CommandHandler{
enum CommandIds : Int, CommandId{
case commandA1
case commandA2
}
}
class HandlerB : CommandHandler{
enum CommandIds : String, CommandId{
case commandB1 = "Command B1"
case commandB2
case commandB3 = "Yet another command"
}
}
这是一个接受 CommandHandler
类型的测试函数
func enumerateCommandIds<T:CommandHandler>(_ commandHandlerType:T.Type){
for value in commandHandlerType.CommandIds.values{
let caseName = String(describing:value)
let caseRawValue = value.rawValue
print("\(caseName) = '\(caseRawValue)'")
}
}
最后,这是运行该测试的结果
enumerateCommandIds(HandlerA.self)
// Outputs
// commandA1 = '0'
// commandA2 = '1'
enumerateCommandIds(HandlerB.self)
// Outputs
// commandB1 = 'Command B1'
// commandB2 = 'commandB2'
// commandB3 = 'Yet another command'
这是一条漫长而多风的路,但我们做到了!感谢大家的帮助!
关于swift - 您如何反射(reflect) Codable/Codable Keys 协议(protocol)的设计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47784254/