Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将字符串值转换为枚举_Ios_Swift_Enums - Fatal编程技术网

Ios 如何将字符串值转换为枚举

Ios 如何将字符串值转换为枚举,ios,swift,enums,Ios,Swift,Enums,如何将字符串值转换为字符串类型的枚举 我的枚举定义如下: enum TemplateSlotType : String { case Default = "" case Recommendation = "recommendation" } 我想将字符串“Recommension”转换为一种类型。Recommension我在堆栈溢出上没有找到我的答案,但找到了我的答案 这也适用于其他枚举类型: enum Lets : String { case X = "x"

如何将字符串值转换为字符串类型的枚举

我的枚举定义如下:

enum TemplateSlotType : String {
    case Default = ""
    case Recommendation = "recommendation"
}

我想将字符串“Recommension”转换为一种类型。Recommension

我在堆栈溢出上没有找到我的答案,但找到了我的答案

这也适用于其他枚举类型:

enum Lets : String {
    case X = "x"
    case Y = "y"
    case Z = "z"
}

var zz = Lets.fromRaw("z")
zz! == .Z // true
println(Lets.Y.toRaw())
有关详细信息,请签出:

我的最后执行:

if let slotType = json["type"].object as? String {
    type = TemplateSlotType(rawValue: slotType)!
}