Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch in Swift - Case label in a switch should have at least one executable statement

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 ""
        }
    }
}

enter image description here

I'm pretty sure it's a simple mistake, anyone see it?

like image 413
Francescu Avatar asked Nov 25 '14 16:11

Francescu


1 Answers

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
    }
}
like image 163
Cenny Avatar answered Sep 24 '22 19:09

Cenny