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

3

14

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?

Aorist answered 25/11, 2014 at 16:1 Comment(0)
M
12

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
    }
}
Melli answered 25/11, 2014 at 16:38 Comment(0)
M
22

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

Marmoreal answered 25/11, 2014 at 16:3 Comment(5)
Ok ty, and I found that you can shortcut that with a comma. case .value1, .value2:. I forgot it, but this was in ebook first chapters. (I'll accept this answer asap)Aorist
Yeah that's another way, but for a long list I prefer an explicit one-by-one list, although at the expense of more code - that's more readable at my eyes, but it's just a matter of preference. Nice to have more than one way to do the same thing though.Marmoreal
@Aorist why didn't you selected Cenny's answer then? that one should be the corrected answer. This one shouldn'tCommunard
@JandroRojas I don't really like your tone tbh. The answer is 5+ years old, it was accepted before the other one was written and still, is technically correct.Aorist
@Aorist I don't know what tone are you referring to tbh but this answer should not be considered correct (not even technically)... yes it works but it's basically the same as copying and pasting the content of each case block. It's always a shame when the correct answer has fewer upvotes than a "technically correct" one...Communard
M
12

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
    }
}
Melli answered 25/11, 2014 at 16:38 Comment(0)
S
0

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
}
Steiger answered 16/6, 2023 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.