Switch in Swift - Case label in a switch should have at least one executable statement(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 标签应该至少有一个可执行语句
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01