如何将String值转换为String类型的枚举?
我的枚举定义如下:
enum TemplateSlotType : String {
case Default = ""
case Recommendation = "recommendation"
}
我想将字符串“recommendation”转换为类型。Recommendation
最佳答案
我没有在Stack Overflow上找到答案,但是找到了答案。
这也适用于其他枚举类型:
enum Lets : String {
case X = "x"
case Y = "y"
case Z = "z"
}
var zz = Lets.fromRaw("z")
zz! == .Z // true
println(Lets.Y.toRaw())
有关更多信息,请结帐:http://ericasadun.com/2014/08/27/swift-converting-values-to-enumerations/
我的最终实现:
if let slotType = json["type"].object as? String {
type = TemplateSlotType(rawValue: slotType)!
}
关于ios - 如何将字符串值转换为枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27355374/