【发布时间】:2018-01-18 17:11:02
【问题描述】:
我正在尝试为我经常遇到的问题找到最佳解决方案,但我每次都以不同的方式解决。
想象一下我有一个分几个步骤的表格(假设是 2 开始)
我的代码结构是:
class SuperStepViewController: UIViewController {
//Some generic Stuff
func continueAction(sender : AnyObject?) {
//NOTHING
}
}
class Step1ViewController: SuperStepViewController {
override func continueAction(sender : AnyObject?) {
//DO SOME USEFULL STUFF
}
}
class Step2ViewController: SuperStepViewController {
override func continueAction(sender : AnyObject?) {
//DO SOME USEFULL STUFF
}
}
我想要更改此代码,不实现SuperViewController 中的continueAction 函数,因为它没有默认实现。
乍一看,我认为protocol 是个好主意。如果我将continueAction 放入所需的协议中,我将遇到编译时错误,这正是我想要的。
protocol StepProtocol {
func continueAction()
}
class SuperStepViewController: UIViewController {
//GENERIC
}
class Step1ViewController: SuperStepViewController, StepProtocol {
func continueAction(sender : AnyObject?) {
//DO SOME USEFULL STUFF
}
}
class Step2ViewController: SuperStepViewController, StepProtocol {
func continueAction(sender : AnyObject?) {
//DO SOME USEFULL STUFF
}
}
但这还不够,我想在子类化 superview 控制器后立即生成此编译。我知道 Java 类似于抽象类。
class Step3ViewController: SuperStepViewController {
//NO continueAction implementation => No compilation error
}
有人有想法吗?
【问题讨论】:
-
我试过你的代码,它似乎像你想要的那样工作。一旦我将 SuperStepViewController 和 StepProtocol 子类化,我就被告知它需要“continueAction”-function 并建议“internal func continueAction()”。如果我只继承 SuperStepViewController 而不是协议,我当然不会出错。这不是你想要的吗?
-
不,我不想明确扩展 stepProtocol。但我认为这毕竟是不可能的
标签: ios swift architecture