I have an objc base class:
@protocol BaseClassDelegate;
@interface BaseClass : NSObject
@property (nonatomic, weak) id delegate;
I'd either create a wrapper delegate to make it the correct type in SubClass
.
class SubClass: BaseClass {
var myDelegate: SubClassDelegate? {
get { return delegate as? SubClassDelegate }
set { delegate = newValue }
}
@IBAction func onDoSomething(sender: AnyObject) {
myDelegate?.additionalSubClassDelegateMethod();
}
}
Or simply cast the delegate to the expected type:
(delegate as? SubClassDelegate)?.additionalSubClassDelegateMethod();
Here's a more comprehensive example of how to do this. Thanks to redent84 for pointing me in the right direction.
protocol SubclassDelegate: ClassDelegate {
func subclassDelegateMethod()
}
class Subclass: Class {
// here we assume that super.delegate property exists
@IBAction func buttonPressedOrSomeOtherTrigger() {
if let delegate: SubclassDelegate = self.delegate as? SubclassDelegate {
delegate.subclassDelegateMethod()
}
}
}
And then in your implementation:
extension SomeOtherClass: SubclassDelegate {
let someObject = Subclass()
someObject.delegate = self
func subclassDelegateMethod() {
// yay!
}
}