I have string enum:
enum Country:String {
case France
case Germany
case UnitedStates
}
But depending on the uibutton restorationIdentifier I want to do something.
I have this ibaction:
@IBAction func countrySelection(_ sender: UIButton) {
guard let selection:String = sender.restorationIdentifier else { return}
switch selection {
case Country.France:
default:
return
}
}
But I'm getting this error on this line:
Enum case 'France' is not a member of type 'String'
line of code:
case Country.France:
I can fix the error changing that line to:
case Country.France.rawValue
But my question is, why do I need to or force to use the raw value?
I'll really appreciate your help.
You're trying to compare a String
to a Country
value. They're not the same type. As you noted, you could change your switch
cases
to String
: Country.France.rawValue
.
Or you can convert the String
into a Country
value:
@IBAction func countrySelection(_ sender: UIButton) {
guard let selection = sender.restorationIdentifier else { return }
guard let country = Country(rawValue: selection)
switch country {
case .France:
// handle France
default:
return
}
}
Note: That isn't really what the restorationIdentifier is for.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.