Strings in Switch Statements: 'String' does not conform to protocol 'IntervalType'

前端 未结 5 953
我在风中等你
我在风中等你 2021-02-05 00:40

I am having problems using strings in switch statements in Swift.

I have a dictionary called opts which is declared as

5条回答
  •  时光取名叫无心
    2021-02-05 01:26

    Instead of the unsafe force unwrap.. I find it easier to test for the optional case:

    switch opts["type"] {
      case "abc"?:
        println("Type is abc")
      case "def"?:
        println("Type is def")
      default:
        println("Type is something else")
    }
    

    (See added ? to the case)

提交回复
热议问题