切换Swift - 交换机中的案例标签应至少有一个可执行语句 [英] Switch in Swift - Case label in a switch should have at least one executable statement
问题描述
我有一个枚举
类型,在Swift中扩展 String
。
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
这是我的代码:
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?
推荐答案
在快速开关
语句中没有隐含的突破,所以你必须明确地设置: p>
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.
请注意,swift要求每个交换机案例至少包含一条语句 - 如果没有语句必须使用明确的 break
(在这种情况下,这意味着do nothing)
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")
这篇关于切换Swift - 交换机中的案例标签应至少有一个可执行语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!