3

Is there a way to write this if/else if/else ladder as a switch statement?

let x: Any = "123"

if let s = x as? String {
    useString(s)
}
else if let i = x as? Int {
    useInt(i)
}
else if let b = x as? Bool {
    useBool(b)
}
else {
    fatalError()
}

Here's my attempt:

switch x {
case let s where s is String:   useString(s)
case let i where i is Int:      useInt(i)
case let b where b is Bool:     useBool(b)
default: fatalError()
}

It successfully chooses the right path, but s/i/b are still of type Any. The is check doesn't have any effect in casting them. This forces me to force cast with as! before usage.

Is there a way to switch on the type, and bind it to a name, all in one switch statement?

Alexander
  • 59,041
  • 12
  • 98
  • 151
  • What you are doing is correct but missing executable statements for each `case`. Just add a `print` and see you wont see any errors – Santosh Aug 16 '16 at 17:00
  • Yes, that's an error, but I omitted it for the sake of demonstration. `print(_:)` has no problem handling parameters of type `Any`. Imagine the comments like `//use s` are actually function calls that take a parameter of type `String`/`Int`/`Bool`, respectively. – Alexander Aug 16 '16 at 17:02
  • You said - "but s/i/b are still of type Any. The `is` check doesn't have any effect in casting them" So if you really print `s/i/b` then actually it prints the right value. `print(s)`. I didn't knw about your function `useString`. Thanks – Santosh Aug 16 '16 at 17:09
  • Actually it is demonstrated as an example in the Swift Book: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html#//apple_ref/doc/uid/TP40014097-CH22-ID338, search for `for thing in things` ... – Martin R Aug 16 '16 at 17:12

1 Answers1

11

Sure, you can use the conditional casting pattern case let x as Type:

let x: Any = "123"

switch x {
case let s as String:
    print(s)   //use s
case let i as Int:
    print(i)   //use i
case let b as Bool:
    print(b)   //use b
default:
    fatalError()
}
Hamish
  • 78,605
  • 19
  • 187
  • 280