最佳答案
我尝试从原始值中获取枚举类型:
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
但 rawValue
似乎不适用于带空格的字符串:
let birth = TestEnum(rawValue: "Birth Day") //nil
有什么建议吗?