Reputation: 10139
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?
Upvotes: 42
Views: 48024
Reputation: 3716
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)
Upvotes: 6
Reputation: 3154
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
Upvotes: 68
Reputation: 1550
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.
Upvotes: 39
Reputation: 7922
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
}
}
Upvotes: 13
Reputation: 27345
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")
}
Upvotes: 17