I have done this:
func doSomething(anObject: AnyObject)
{
switch anObject {
case let myObj as MyClass:
println("Is a kind of MyClass")
case let yourObj as YourClass:
println("Is a kind of YourClass")
default:
break
}
}
Which works as you'd expect, classes and subclasses of MyClass
or YourClass
cause the relevant print statements to be executed.
However, I have a case where I want it to match an exact class (not any subclass of that class). Ideally I want something as simple and elegant as the case let myObj as MyClass:
line, something like case let myObj as exactly MyClass:
. Is something like this possible in Swift? Otherwise, what is the most elegant and concise way to achieve this within a case statement?