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

前端 未结 5 942
我在风中等你
我在风中等你 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)

    0 讨论(0)
  • 2021-02-05 01:29

    According to Swift Language Reference:

    The type Optional is an enumeration with two cases, None and Some(T), which are used to represent values that may or may not be present.

    So under the hood an optional type looks like this:

    enum Optional<T> {
      case None
      case Some(T)
    }
    

    This means that you can go without forced unwrapping:

    switch opts["type"] {
    case .Some("A"):
      println("Type is A")
    case .Some("B"):
      println("Type is B")
    case .None:
      println("Type not found")
    default:
      println("Type is something else")
    }
    

    This may be safer, because the app won't crash if type were not found in opts dictionary.

    0 讨论(0)
  • 2021-02-05 01:32

    Try using:

    let str:String = opts["type"] as String
    switch str {
    case "abc":
        println("Type is abc")
    case "def":
        println("Type is def")
    default:
        println("Type is something else")
    }
    
    0 讨论(0)
  • 2021-02-05 01:33

    I had this same error message inside prepareForSegue(), which I imagine is fairly common. The error message is somewhat opaque but the answers here got me on the right track. If anyone encounters this, you don't need any typecasting, just a conditional unwrap around the switch statement:-

    if let segueID = segue.identifier {
        switch segueID {
            case "MySegueIdentifier":
                // prepare for this segue
            default:
                break
        }
    }
    
    0 讨论(0)
  • 2021-02-05 01:35

    This error is shown when an optional is used in a switch statement. Simply unwrap the variable and everything should work.

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

    Edit: In case you don't want to do a forced unwrapping of the optional, you can use guard. Reference: Control Flow: Early Exit

    0 讨论(0)
提交回复
热议问题