In Swift, is it possible to convert a string to an enum?

后端 未结 8 965
[愿得一人]
[愿得一人] 2020-12-14 05:17

If I have an enum with the cases a,b,c,d is it possible for me to cast the string \"a\" as the enum?

8条回答
  •  再見小時候
    2020-12-14 05:46

    All you need is:

    enum Foo: String {
       case a, b, c, d
    }
    
    let a = Foo(rawValue: "a")
    assert(a == Foo.a)
    
    let 

提交回复
热议问题