In Swift you can check the class type of an object using 'is'. How can I incorporate this into a 'switch' block?
I think it's not possible, so I'm wondering what is the best way around this.
TIA, Peter.
In Swift you can check the class type of an object using 'is'. How can I incorporate this into a 'switch' block?
I think it's not possible, so I'm wondering what is the best way around this.
TIA, Peter.
I like this syntax:
since it gives you the possibility to extend the functionality fast, like this:
In case you don't have a value, just any class:
Putting up the example for "case is - case is Int, is String:" operation, where multiple cases can be used clubbed together to perform the same activity for Similar Object types. Here "," separating the types in case is operating like a OR operator.
Demo Link
You absolutely can use
is
in aswitch
block. See "Type Casting for Any and AnyObject" in the Swift Programming Language (though it's not limited toAny
of course). They have an extensive example: