跳至主要内容

博文

目前显示的是 七月, 2021的博文

nested generics in swift

 原文链接 Yes, you can nest generics. The problem with what you have is this line: class BaseService < T : BaseVM < BaseModel >> { ... Here you've said  BaseService  must be initialized with a type that inherits from  BaseVM  whose generic  IS   BaseModel . If you want  BaseService  to be able to take a type that inherits from  BaseVM  whose model inherits from  BaseModel , you'd have to do it this way: class BaseService < T : BaseModel , U : BaseVM < T >> { ... Here is a version of what you have above that compiles: class BaseModel {} class BaseVM < T : BaseModel > {} class BaseService < T : BaseModel , U : BaseVM < T >> { //init viewmodel with generic model } class Human : BaseModel { var name = "" } class HumanVM : BaseVM < Human > { var name = "" init ( model : Human ) { super . init () name = model.name } } class Huma...