I have a class with a delegate of type UIViewController
This delegate can be one of 2 subclasses of UIViewController. Both subclasses contain a method with the same name taking the same arguments.
class TypeOne: UIViewController {
method() {
}
}
class TypeTwo: UIViewController {
method() {
}
}
Currently I'm writing the statement like this and of course it works, but it's driving me batty from a DRY standpoint.
if let delegate = delegate as? TypeOne {
delegate.method()
} else if let delegate = delegate as? TypeTwo {
delegate.method()
}
I want to do something like
if let delegate = delegate as? TypeOne ?? delegate as TypeTwo {
delegate.method()
}
But the above doesn't actually downcast the delegate, as I get an error that type UIViewController doesn't contain 'method'
How else can I chain this so if the first downcast fails, the second one is tried and the delegate is treated as either type rather than a base UIViewController
?