从基础扩展委托 class
Extending a delegate from a base class
我有一个 objc 基础 class:
@protocol BaseClassDelegate;
@interface BaseClass : NSObject
@property (nonatomic, weak) id <BaseClassDelegate> delegate;
@end
@protocol BaseClassDelegate <NSObject>
-(void)baseDelegateMethod;
@end
我正在创建一个 swift 子class,我想在其中扩展我的委托...
protocol SubClassDelegate : BaseClassDelegate {
func additionalSubClassDelegateMethod();
}
class SubClass: BaseClass {
@IBAction func onDoSomething(sender: AnyObject) {
delegate?.additionalSubClassDelegateMethod(); <--- No such method in 'delegate'
}
}
现在,当我创建子 class 时,我可以说它符合 SubClassDelegate 并设置委托。问题是(当然)是 'delegate' 在这个子 class 中不存在。有没有办法告诉编译器将 'extend' 我的委托转换为我的子 class? (或者我在这里疯了,错过了一些明显的东西)
我要么创建一个包装委托,使其成为 SubClass
中的正确类型。
class SubClass: BaseClass {
var myDelegate: SubClassDelegate? {
get { return delegate as? SubClassDelegate }
set { delegate = newValue }
}
@IBAction func onDoSomething(sender: AnyObject) {
myDelegate?.additionalSubClassDelegateMethod();
}
}
或者简单地将委托转换为预期的类型:
(delegate as? SubClassDelegate)?.additionalSubClassDelegateMethod();
下面是有关如何执行此操作的更全面的示例。感谢 redent84 为我指明了正确的方向。
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()
}
}
}
然后在您的实施中:
extension SomeOtherClass: SubclassDelegate {
let someObject = Subclass()
someObject.delegate = self
func subclassDelegateMethod() {
// yay!
}
}
我有一个 objc 基础 class:
@protocol BaseClassDelegate;
@interface BaseClass : NSObject
@property (nonatomic, weak) id <BaseClassDelegate> delegate;
@end
@protocol BaseClassDelegate <NSObject>
-(void)baseDelegateMethod;
@end
我正在创建一个 swift 子class,我想在其中扩展我的委托...
protocol SubClassDelegate : BaseClassDelegate {
func additionalSubClassDelegateMethod();
}
class SubClass: BaseClass {
@IBAction func onDoSomething(sender: AnyObject) {
delegate?.additionalSubClassDelegateMethod(); <--- No such method in 'delegate'
}
}
现在,当我创建子 class 时,我可以说它符合 SubClassDelegate 并设置委托。问题是(当然)是 'delegate' 在这个子 class 中不存在。有没有办法告诉编译器将 'extend' 我的委托转换为我的子 class? (或者我在这里疯了,错过了一些明显的东西)
我要么创建一个包装委托,使其成为 SubClass
中的正确类型。
class SubClass: BaseClass {
var myDelegate: SubClassDelegate? {
get { return delegate as? SubClassDelegate }
set { delegate = newValue }
}
@IBAction func onDoSomething(sender: AnyObject) {
myDelegate?.additionalSubClassDelegateMethod();
}
}
或者简单地将委托转换为预期的类型:
(delegate as? SubClassDelegate)?.additionalSubClassDelegateMethod();
下面是有关如何执行此操作的更全面的示例。感谢 redent84 为我指明了正确的方向。
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()
}
}
}
然后在您的实施中:
extension SomeOtherClass: SubclassDelegate {
let someObject = Subclass()
someObject.delegate = self
func subclassDelegateMethod() {
// yay!
}
}