Reputation: 21
I'm try to play with swift and enum. My concern is that Enum in swift is the Value data type. So can we have initializers in this like structure or class.
Upvotes: 1
Views: 173
Reputation: 10136
Yes, we can:
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"
Upvotes: 2