I have an enum
type that extends String
in Swift.
When I try to use a switch
I got an error:
Case label in a switch should have at least one executable statement
Here is my code:
enum UserInfosKey:String {
case CameraMyPhotoStream = "CMPS"
case CameraICloudActivated = "CICA"
case CameraICloudShare = "CICS"
case ProjectTodayExtension = "PTE"
case ProjectShareExtension = "PSE"
case NetworkConnection = "NC"
case PhoneLanguage = "PL"
case CameraPhotosCount = "CPC"
case UserIdentifier = "UI"
case VersionHistory = "VH"
case Path = "Path"
}
class UserInfosController: NSObject {
func update(key:UserInfosKey, value:String, context:UserDefaultsMainKeys) -> String {
switch key {
case .CameraICloudActivated:
case .CameraICloudShare:
case .CameraMyPhotoStream:
case .CameraPhotosCount:
case .NetworkConnection:
case .PhoneLanguage:
case .UserIdentifier:
return value
default:
return ""
}
}
}
I'm pretty sure it's a simple mistake, anyone see it?
You can have many values for a case, all you have to do is to separate them by a comma.
I would also recommend returning an nil value than an empty string and make the function return value an String?, but that depends on how the function is going to be used.
func update(key:UserInfosKey, value:String, context:UserDefaultsMainKeys) -> String? {
switch key {
case .CameraICloudActivated,
.CameraICloudShare,
.CameraMyPhotoStream,
.CameraPhotosCount,
.NetworkConnection,
.PhoneLanguage,
.UserIdentifier:
return value
default:
return nil
}
}
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