Francescu
Francescu

Reputation: 17054

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?

Upvotes: 14

Views: 9795

Answers (3)

MichaelLedger
MichaelLedger

Reputation: 1

You could use 'break' or 'fallthrough' to fix it!

func update(key:UserInfosKey, value:String, context:UserDefaultsMainKeys) -> String? {
    var newValue: String?
    switch key {
    case .CameraICloudActivated, 
         .CameraICloudShare, 
         .CameraMyPhotoStream,
         .CameraPhotosCount, 
         .NetworkConnection, 
         .PhoneLanguage, 
         .UserIdentifier:
        newValue = value  
    default:
        break
    }
    return newValue
}

Upvotes: 0

Cenny
Cenny

Reputation: 1984

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

Upvotes: 12

Antonio
Antonio

Reputation: 72760

There is no implicit fallthrough in the swift switch statement, so you have to explicitly set that:

    case .CameraICloudActivated: fallthrough
    case .CameraICloudShare: fallthrough
    case .CameraMyPhotoStream: fallthrough
    case .CameraPhotosCount: fallthrough
    case .NetworkConnection: fallthrough
    case .PhoneLanguage: fallthrough
    case .UserIdentifier:
        return value

Without that, each case has implicit break.

Note that swift requires that each switch case contains at least one statement - in case of no statement, an explicit break must be used (which in this case means "do nothing")

Upvotes: 22

Related Questions