我想弄清楚是否有可能通过枚举的类型传递相同的方式,你可以在Swift中传递Class
对象。可能传递一个枚举类型名称作为参数在Swift中?
我的实际使用情况比这更复杂一些,但对于讨论,让我们说,我有两个Int
枚举:
enum Foo: Int, CustomStringConvertible {
case firstFoo = 0
case anotherFoo = 1
var description: String {
switch self {
case .firstFoo:
return "Hello Foo"
case .anotherFoo:
return "Goodbye Foo"
}
}
}
enum Bar: Int, CustomStringConvertible {
case firstBar = 0
case anotherBar = 1
var description: String {
switch self {
case . firstBar:
return "Hello Bar"
case . anotherBar:
return "Goodbye Bar"
}
}
}
我希望能够写这样的函数:
func justAnExample(whichEnum: enum) {
let val = whichEnum(rawValue: 0)
print("description: \(String(val))")
}
,然后用它是这样的:
justAnExample(Foo)
// prints: "description: Hello Foo"
justAnExample(Bar)
// prints: "description: Hello Bar"
这可能吗?如果是这样,函数声明中whichEnum
的签名是什么?
很好的链接到SO文档+1 –