简体   繁体   English

如何在Swift的泛型中声明基类?

[英]How to declare base classes in Swift's generics?

Example of code which doesn't work, but I would like to have it compiled with success. 代码示例不起作用,但我希望编译成功。 Idea is described very nicely in following source code. 在下面的源代码中很好地描述了Idea。 You can paste it into Swift playground easily. 您可以轻松地将其粘贴到Swift游乐场。

import UIKit

// MARK: - Base classes with generics

class FlowController {

}

class ViewModel<FlowControllerType: FlowController> {
    let flowController: FlowControllerType
    init (flowController: FlowControllerType) {
        self.flowController = flowController
    }
}

class ViewController<ViewModelType: ViewModel<FlowController>> {

    var viewModel: ViewModelType!
}

// MARK: - My implementation

class MyFlowController: FlowController {

}

class MyViewModel: ViewModel<MyFlowController> {
    override init (flowController: MyFlowController) {
        super.init(flowController: flowController)
    }
}

// ERROR: 'ViewController' requires that 'MyModelView' inherit from 'ViewModel<FlowController>'
class MyViewController: ViewController<MyViewModel> {

}

// MARK: - Code

let viewController = MyViewController()
viewController.viewModel = MyViewModel(flowController: MyFlowController())

Problem is that I would like to declare MyViewController , but I get error: 问题是我想声明MyViewController ,但是我收到错误:

'ViewController' requires that 'MyModelView' inherit from 'ViewModel<FlowController>'

Question is how to declare these classes to make it working? 问题是如何声明这些类以使其工作? Should I use where clause for this? 我应该使用where子句吗? I want to have my own types for ViewController.viewModel and ViewModel.flowController in base classes keeping these dependencies between classes. 我希望在基类中拥有自己的ViewController.viewModelViewModel.flowController类型,以保持类之间的这些依赖关系。 Thank you in advance. 先感谢您。

Related question: Swift generic inheritance 相关问题: Swift通用继承

I have probably answer, but it could look better than this solution. 我可能会回答,但它看起来比这个解决方案更好。 Trick is in following line 诀窍在于以下几行

class ViewController<FlowControllerType: FlowController, ViewModelType: ViewModel<FlowControllerType>> {

which should be used instead of 应该用来代替

class ViewController<ViewModelType: ViewModel<FlowController>> {

I hope somebody else will do it better, but for now only this works. 我希望别人会做得更好,但现在只有这个有效。 Full playground example: 完整的操场示例:

import UIKit

// MARK: - Base classes with generics

class FlowController {

}

class ViewModel<FlowControllerType: FlowController> {
    let flowController: FlowControllerType
    init (flowController: FlowControllerType) {
        self.flowController = flowController
    }
}

class ViewController<FlowControllerType: FlowController, ViewModelType: ViewModel<FlowControllerType>> {

    var viewModel: ViewModelType!
}

// MARK: - My implementation

class MyFlowController: FlowController {

}

class MyViewModel: ViewModel<MyFlowController> {
    override init (flowController: MyFlowController) {
        super.init(flowController: flowController)
    }
}

class MyViewController: ViewController<MyFlowController, MyViewModel> {

}

// MARK: - Code

let viewController = MyViewController()
viewController.viewModel = MyViewModel(flowController: MyFlowController())

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

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