我可以通过[]访问枚举吗?

时间:2018-08-29 09:02:43

标签: swift enums

如何使用[](如Numbers [“ one”])访问枚举:

enum Numbers: String {
  case one = "1"
  case two = "2"
  case three =  "3"
}

print(Numbers["one"])

3 个答案:

答案 0 :(得分:1)

let access_num = Numbers.two
print(access_num.rawValue)
// prints "2"

enum Numbers: Int {
 case one = 1, two , three , four
} 

var access_num = Numbers(rawValue: 2)!
print(access_num)

答案 1 :(得分:0)

也许您是说rawValue吗?

print(Numbers(rawValue:"1"))

答案 2 :(得分:0)

您可以像这样访问其字符串值:

print(Numbers.one.rawValue)。 //输出:-“ 1 / n”