我想弄清楚是否有可能通過枚舉的類型傳遞相同的方式,你可以在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 –