简体   繁体   English

iOS Swift 3体系结构问题

[英]iOS Swift 3 Architecture issue

I'm trying to find the best solution for a recurrent problem I have, which I solved differently each time. 我正在尝试为遇到的反复出现的问题找到最佳的解决方案,每次我都会以不同的方式解决。

Imagine I have a form in several steps ( let say 2 to begin ) 想象一下,我有几个步骤的表单(假设2开始)

My code structure is : 我的代码结构是:

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
    }

}

What I want is to change this code, to not implement the continueAction function in the SuperViewController , because it has no default implementation. 我想要的是更改此代码,以不实现SuperViewControllercontinueAction函数,因为它没有默认实现。

At a first look, I thought protocol was a good idea. 乍一看,我认为protocol是个好主意。 If I put continueAction in a required protocol, I will have a compilation-time error, which is what I want. 如果将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
    }

}

But it is not enough, I want generate this compilation as soon as I subclass the superview controller. 但这还不够,我想在子类化Superview控制器后立即生成此编译。 I know Java as something like Abstract class. 我知道Java就像Abstract类一样。

class Step3ViewController: SuperStepViewController { 

   //NO continueAction implementation => No compilation error

}

Did anyone have an idea ? 有人知道吗?

No. This is not possible in Swift. 不可以。这在Swift中是不可能的。 Swift does not support such activity. Swift不支持此类活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM