简体   繁体   中英

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

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

I have a dictionary called opts which is declared as <String, AnyObject>

I have this code:

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

and on the lines case "abc" and case "def" I get the following error:

Type 'String' does not conform to protocol 'IntervalType'

Can someone explain to me what I am doing wrong?

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

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.

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")
}

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
    }
}

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM