Switch in Swift - switch 中的 case 标签应该至少有一个可执行语句

Switch in Swift - Case label in a switch should have at least one executable statement(Switch in Swift - switch 中的 case 标签应该至少有一个可执行语句)

本文介绍了Switch in Swift - switch 中的 case 标签应该至少有一个可执行语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 enum 类型,它在 Swift 中扩展了 String.

I have an enum type that extends String in Swift.

当我尝试使用 switch 时出现错误:

When I try to use a switch I got an error:

switch 中的 case 标签应该至少有一个可执行语句

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

我很确定这是一个简单的错误,有人看到了吗?

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

推荐答案

一个case可以有多个值,只需用逗号分隔即可.

You can have many values for a case, all you have to do is to separate them by a comma.

我还建议返回 nil 值而不是空字符串,并使函数返回值 String?,但这取决于函数的使用方式.

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

这篇关于Switch in Swift - switch 中的 case 标签应该至少有一个可执行语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Switch in Swift - switch 中的 case 标签应该至少有一个可执行语句

基础教程推荐