我有一个在
Swift中扩展String的枚举类型.
当我尝试使用开关时出现错误:
Case label in a switch should have at least one executable statement
这是我的代码:
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 "" } } }
我很确定这是一个简单的错误,有人看到了吗?
解决方法
swift switch语句中没有隐含的漏洞,因此您必须明确设置:
case .CameraICloudActivated: fallthrough case .CameraICloudShare: fallthrough case .CameraMyPhotoStream: fallthrough case .CameraPhotosCount: fallthrough case .NetworkConnection: fallthrough case .PhoneLanguage: fallthrough case .UserIdentifier: return value
没有它,每个案例都有隐含的突破.
请注意,swift要求每个switch case包含至少一个语句 – 如果没有语句,则必须使用显式中断(在这种情况下意味着“什么都不做”)