[英]Swift custom UITableViewController: switch statement case label section enum
[英]Switch in Swift - Case label in a switch should have at least one executable statement
我有一個在Swift中擴展String
的enum
類型。
當我嘗試使用switch
出現錯誤:
交換機中的案例標簽應至少具有一個可執行語句
這是我的代碼:
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包含至少一個語句 - 如果沒有語句,則必須使用顯式break
(在這種情況下意味着“什么都不做”)
您可以為案例設置多個值,您只需要用逗號分隔它們即可。
我還建議返回一個nil值而不是一個空字符串,並使函數返回值為String?,但這取決於函數的使用方式。
func update(key:UserInfosKey, value:String, context:UserDefaultsMainKeys) -> String? {
switch key {
case .CameraICloudActivated,
.CameraICloudShare,
.CameraMyPhotoStream,
.CameraPhotosCount,
.NetworkConnection,
.PhoneLanguage,
.UserIdentifier:
return value
default:
return nil
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.