I am having problems using strings in switch statements in Swift.
I have a dictionary called opts
which is declared as
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)
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
}
}
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