How to initialize Enums With Optionals in Swift
Issue #49
Today someone showed me https://medium.com/@_Easy_E/initializing-enums-with-optionals-in-swift-bf246ce20e4c which tries to init enum with optional value.
1 | enum Planet: String { |
One interesting fact about optional, is that it is a monad, so it has map
and flatMap
. Since enum
init(rawValue:)
returns an optional, we need to use flatMap
. It looks like this
1 | let name: String? = "venus" |
🎉