iOS Swift 3 架构问题

标签 ios swift architecture

我正在尝试为我经常遇到的问题找到最佳解决方案,但每次我都以不同的方式解决该问题。

假设我有一个需要几个步骤的表单(假设从 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/

相关文章:

ios - NSIncationOperation - 不在不同线程上调用方法吗?

ios - 如何从日期数组中获取最接近当前时间的时间? iOS, swift 3

asp.net-mvc - 如何构建企业 MVC 应用程序,业务逻辑在哪里?

networking - 微服务扩展/插件架构

iOS 8 : How to use segues

ios - super View 旋转后获取 subview 位置

ios - NSFetchedResultsController 添加新记录时如何防止 UITableView 向上滚动?

ios - 链接无法从 iOS 9.0 中的 safari 应用程序打开,但在 iOS 9.3 中有效

ios - 从下往上填充UIView

cocoa - 通用 Cocoa 应用程序架构编程指南