Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift到Kotlin:协议到接口,带泛型_Swift_Generics_Interface_Kotlin_Protocols - Fatal编程技术网

Swift到Kotlin:协议到接口,带泛型

Swift到Kotlin:协议到接口,带泛型,swift,generics,interface,kotlin,protocols,Swift,Generics,Interface,Kotlin,Protocols,在过去的几天里,我一直在做这个小代码翻译。这真的只是科特林语法难住了我 斯威夫特: protocol BaseProtocol { associatedtype VMType = BaseViewModel var viewModel: VMType { get set } } public class BaseViewModel { var moduleModel: ModuleModel required public init(moduleModel: M

在过去的几天里,我一直在做这个小代码翻译。这真的只是科特林语法难住了我

斯威夫特:

protocol BaseProtocol {
    associatedtype VMType = BaseViewModel
    var viewModel: VMType { get set }
}

public class BaseViewModel {
    var moduleModel: ModuleModel
    required public init(moduleModel: ModuleModel) {
        self.moduleModel = moduleModel
    }
}
以下是我给Kotlin带来的内容(这是让我感到困惑的接口协议,主要涉及泛型)

科特林:

interface BaseInterface<VMType> {    // Not sure if this is correct
    // protocol code...
}

class BaseViewModel(internal var moduleModel: ModuleModel)
interface BaseInterface{//不确定这是否正确
//协议代码。。。
}
类BaseViewModel(内部变量moduleModel:moduleModel)
我相信我已经正确地构建了我的BaseViewModel,但如果这需要调整,我也会洗耳恭听


提前感谢您的建议

我不是一个敏捷的程序员,如果我错了,请纠正我。您的协议类型是否绑定到BaseViewModel类?如果是,您也可以在接口中声明一个有界类型

interface BaseInterface<T : BaseViewModel>

非常好-这似乎满足语法要求。我还没有测试代码,因为它需要在我可以测试之前将其他部分组合在一起。也就是说,我目前的问题已经通过您的投入得到解决。谢谢你的开放式接球!我会搞砸的,完全错过了。
open class BaseViewModel(var moduleModel: ModuleModel)