I want to get the value from an enum after the user selected 'action' in a picker view.
So I have as string: selectedGenre = "action"
.
How can I get "28" from the case?
public enum MovieGenres: String {
case action = "28"
case adventure = "12"
case animation = "16"
...
}
I need to have something like this: MoveGenres.("selectedgenre").rawValue
First of all this is how you define your enum
enum MovieGenre: String {
case action
case adventure
case animation
var code: Int {
switch self {
case .action: return 28
case .adventure: return 12
case .animation: return 16
}
}
}
Now given a string
let stringFromPicker = "action"
You can try to build your enum value
if let movieGenre = MovieGenre(rawValue: stringFromPicker) {
print(movieGenre.code) // 28
}
As you can see I renamed
MovieGenres
asMovieGenre
, infact an enum name should be singular.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With