【发布时间】:2016-12-12 02:57:21
【问题描述】:
我试图弄清楚是否可以像在 Swift 中传递 Class 对象一样传递枚举类型。
我的实际用例比这要复杂一些,但为了讨论,假设我有两个 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的签名是什么?
【问题讨论】: