我正在尝试为我经常遇到的问题找到最佳解决方案,但每次我都以不同的方式解决该问题。
假设我有一个需要几个步骤的表单(假设从 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)中,则会出现编译时错误,这正是我想要的。
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
}
}
但这还不够,我想在子类化 super View Controller 后立即生成此编译。我知道 Java 类似于抽象类。
class Step3ViewController: SuperStepViewController {
//NO continueAction implementation => No compilation error
}
有人有想法吗?
最佳答案
没有。这在 Swift 中是不可能的。 Swift 不支持此类事件。
关于iOS Swift 3 架构问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40466482/