Ios 枚举中值的使用不明确
我已经将我的swift项目更新为swift 4.1,突然我的一些代码显示错误 你能建议我如何处理这些错误吗Ios 枚举中值的使用不明确,ios,swift,Ios,Swift,我已经将我的swift项目更新为swift 4.1,突然我的一些代码显示错误 你能建议我如何处理这些错误吗 import Foundation class HotCase : BaseResponse { enum HotCaseStatus : String { case New = "New" case InProgress = "In Progress" case Cancel = "Cancelled" case
import Foundation
class HotCase : BaseResponse {
enum HotCaseStatus : String {
case New = "New"
case InProgress = "In Progress"
case Cancel = "Cancelled"
case Complete = "Completed"
}
var Id: String?
var STATUS: String?
var CreateDate: String?
var UpdateBy: String?
var TYPE: String?
var CASENUMBER: String?
var REASON: String?
var InternalId: String?
var ORIGIN: String?
var SUBJECT: String?
var DESCRIPTION: String?
var CLOSEDATE: String?
var UpdateDate: String?
var CreateBy: String?
var ACCOUNTID: String?
var isGotDetail: Bool = false
var HotCaseStatus: HotCaseStatus? {
get {
if let state = STATUS {
switch state {
case HotCaseStatus.New.rawValue :
return .New
case HotCaseStatus.InProgress.rawValue :
return .InProgress
case HotCaseStatus.Cancel.rawValue :
return .Cancel
case HotCaseStatus.Complete.rawValue :
return .Complete
default: ()
}
}
return nil
}
}
}
当我试图在case中使用“HotCaseStatus”时,出现了一个错误,显示其用法不明确
多谢各位
var hotCaseStatus: HotCaseStatus? {
get {
if let state = STATUS {
switch state {
case HotCaseStatus.New.rawValue :
return .New
case HotCaseStatus.InProgress.rawValue :
return .InProgress
case HotCaseStatus.Cancel.rawValue :
return .Cancel
case HotCaseStatus.Complete.rawValue :
return .Complete
default: ()
}
}
return nil
}
}
您正在使用与枚举名相同的属性名,因此,您会遇到问题。如果您遵守命名约定,即变量名以小写字母开头,结构名和枚举名以大写字母开头,则很容易避免此类术语混淆
- 类型为
李>HotCaseStatus
- 变量名应该是
hotCaseStatus
var hotCaseStatus: HotCaseStatus? {
return HotCaseStatus(rawValue: STATUS ?? "")
}
您的命名很混乱,我认为这就是问题所在,请尝试
var hotCaseStatus:hotCaseStatus?
非常感谢您的回复。我已将变量名更改为hotCaseStatus。如果我使用您的代码,您能建议我如何在switch和case中使用它吗?您不需要开关。可以将字符串(原始值)转换为枚举,然后返回枚举大小写,否则将返回nil
。谢谢,我会尝试让您知道结果。我已尝试使用您的方法,现在似乎工作正常。你能纠正我是否理解正确吗?在这种情况下,我可以用你的方式代替switch和case right。是的,我的建议与switch
语句完全相同。