我尝试使用 swift 和 enum。我担心的是 swift 中的 Enum 是 Value 数据类型。那么我们可以在这样的结构或类中使用初始值设定项吗?
最佳答案
是的,我们可以:
enum Foo {
case Bar, Qux
init?(_ string: String) {
switch string {
case "Bar":
self = .Bar
case "Qux":
self = .Qux
default:
return nil
}
}
}
let foo = Foo("Bar")!
print(foo) // prints "Bar"
关于ios - 我们可以在 Swift 的 Enum 中使用初始值设定项吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35456183/