ios – 切换到Swift – 交换机中的Case标签应该至少有一个可执行语句

前端之家收集整理的这篇文章主要介绍了ios – 切换到Swift – 交换机中的Case标签应该至少有一个可执行语句前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个在 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包含至少一个语句 – 如果没有语句,则必须使用显式中断(在这种情况下意味着“什么都不做”)

原文链接:https://www.f2er.com/iOS/329121.html

猜你在找的iOS相关文章