I'm trying to get enum type from raw value:
我试图从原始值中获取enum类型:
enum TestEnum: String {
case Name
case Gender
case Birth
var rawValue: String {
switch self {
case .Name: return "Name"
case .Gender: return "Gender"
case .Birth: return "Birth Day"
}
}
}
let name = TestEnum(rawValue: "Name") //Name
let gender = TestEnum(rawValue: "Gender") //Gender
But it seems that rawValue
doesn't work for string with spaces:
但似乎rawValue对于带空格的字符串不起作用:
let birth = TestEnum(rawValue: "Birth Day") //nil
Any suggestions how to get it?
有什么建议吗?
3 个解决方案
#1
65
Too complicated, just assign the raw values directly to the cases
太复杂了,只需将原始值直接赋给这些情况
enum TestEnum: String {
case Name = "Name"
case Gender = "Gender"
case Birth = "Birth Day"
}
let name = TestEnum(rawValue: "Name")! //Name
let gender = TestEnum(rawValue: "Gender")! //Gender
let birth = TestEnum(rawValue: "Birth Day")! //Birth
If the case name matches the raw value you can even omit it
如果大小写名称与原始值匹配,您甚至可以省略它
enum TestEnum: String {
case Name, Gender, Birth = "Birth Day"
}
In Swift 3+ all enum cases are lowercased
在Swift 3+中,所有enum用小写形式表示
#2
7
Full working example:
完整的工作示例:
enum TestEnum: String {
case name = "A Name"
case otherName
case test = "Test"
}
let first: TestEnum? = TestEnum(rawValue: "A Name")
let second: TestEnum? = TestEnum(rawValue: "OtherName")
let third: TestEnum? = TestEnum(rawValue: "Test")
print("\(first), \(second), \(third)")
All of those will work, but when initializing using a raw value it will be an optional. If this is a problem you could create an initializer or constructor for the enum to try and handle this, adding a none
case and returning it if the enum couldn't be created. Something like this:
所有这些都可以工作,但是当使用原始值初始化时,它将是可选的。如果这是一个问题,您可以为enum创建一个初始化器或构造函数来尝试和处理它,添加一个none case并在未能创建enum时返回它。是这样的:
static func create(rawValue:String) -> TestEnum {
if let testVal = TestEnum(rawValue: rawValue) {
return testVal
}
else{
return .none
}
}
#3
3
You can define enum like this -
你可以像这样定义enum -
enum TestEnum: String {
case Name, Gender, Birth
}
OR
或
enum TestEnum: String {
case Name
case Gender
case Birth
}
you can provide an init method which defaults to one of the member values.
您可以提供一个init方法,该方法默认为其中一个成员值。
enum TestEnum: String {
case Name, Gender, Birth
init() {
self = .Gender
}
}
In the example above, TestEnum.Name has an implicit raw value of "Name", and so on.
在上面的示例中,TestEnum。Name具有“Name”之类的隐式原始值。
You access the raw value of an enumeration case with its rawValue property:
您可以使用其rawValue属性访问枚举实例的原始值:
let testEnum = TestEnum.Name.rawValue
// testEnum is "Name"
let testEnum1 = TestEnum()
// testEnum1 is "Gender"
#1
65
Too complicated, just assign the raw values directly to the cases
太复杂了,只需将原始值直接赋给这些情况
enum TestEnum: String {
case Name = "Name"
case Gender = "Gender"
case Birth = "Birth Day"
}
let name = TestEnum(rawValue: "Name")! //Name
let gender = TestEnum(rawValue: "Gender")! //Gender
let birth = TestEnum(rawValue: "Birth Day")! //Birth
If the case name matches the raw value you can even omit it
如果大小写名称与原始值匹配,您甚至可以省略它
enum TestEnum: String {
case Name, Gender, Birth = "Birth Day"
}
In Swift 3+ all enum cases are lowercased
在Swift 3+中,所有enum用小写形式表示
#2
7
Full working example:
完整的工作示例:
enum TestEnum: String {
case name = "A Name"
case otherName
case test = "Test"
}
let first: TestEnum? = TestEnum(rawValue: "A Name")
let second: TestEnum? = TestEnum(rawValue: "OtherName")
let third: TestEnum? = TestEnum(rawValue: "Test")
print("\(first), \(second), \(third)")
All of those will work, but when initializing using a raw value it will be an optional. If this is a problem you could create an initializer or constructor for the enum to try and handle this, adding a none
case and returning it if the enum couldn't be created. Something like this:
所有这些都可以工作,但是当使用原始值初始化时,它将是可选的。如果这是一个问题,您可以为enum创建一个初始化器或构造函数来尝试和处理它,添加一个none case并在未能创建enum时返回它。是这样的:
static func create(rawValue:String) -> TestEnum {
if let testVal = TestEnum(rawValue: rawValue) {
return testVal
}
else{
return .none
}
}
#3
3
You can define enum like this -
你可以像这样定义enum -
enum TestEnum: String {
case Name, Gender, Birth
}
OR
或
enum TestEnum: String {
case Name
case Gender
case Birth
}
you can provide an init method which defaults to one of the member values.
您可以提供一个init方法,该方法默认为其中一个成员值。
enum TestEnum: String {
case Name, Gender, Birth
init() {
self = .Gender
}
}
In the example above, TestEnum.Name has an implicit raw value of "Name", and so on.
在上面的示例中,TestEnum。Name具有“Name”之类的隐式原始值。
You access the raw value of an enumeration case with its rawValue property:
您可以使用其rawValue属性访问枚举实例的原始值:
let testEnum = TestEnum.Name.rawValue
// testEnum is "Name"
let testEnum1 = TestEnum()
// testEnum1 is "Gender"