我有一个继承自NSOperation的类和一个类应符合的协议:
protocol OperationProtocol { }
class Operation : NSOperation, OperationProtocol {}
我想让每个操作都有一个在协议中实现的main()方法,当从队列运行操作时调用该方法 .
我试过这个:
extension OperationProtocol where Self: NSOperation {
func main() {
doMyStuff()
}
}
不调用此主要方法 . 在它引发编译器错误之前放置“覆盖”(方法不会覆盖超类中的方法)
我能做到的唯一方法是将实现函数命名为“main()”,然后在Operation中覆盖main()并从Protocol调用实现
class Operation : NSOperation, OperationProtocol {
override func main() {
self.elseThanMain()
}
}
但我不想在Operation的每个子类中覆盖main() .
这甚至可能吗?
谢谢