从非协议继承,非 class 类型 'V'

Inheritance from non-protocol, non-class type 'V'

我正在尝试定义一个 class 喜欢

class x<V,M : V> {}

在定义扩展 x 的新 class 时,哪个 V 将成为协议 但是编译器说:

Inheritance from non-protocol, non-class type 'V'

我的真实例子:

class ListController
    <RequestModel,  FillerProtocol , ResultModel, Cell>: BaseViewController
    where
    ResultModel : FillerProtocol,
    Cell: BaseCell<FillerProtocol>,
    RequestModel: ExtendableByBaseListRequestModel {}

导致此错误的原因:

Type 'ResultModel' constrained to non-protocol, non-class type 'FillerProtocol'

使用泛型无法实现您想要实现的目标。首先,您必须能够表达您的通用参数 V 是协议或非最终 class。这不可能。但是这个限制对于约束 M: V 有意义是必要的。

您可能更幸运地使用具有关联类型的协议对您的问题进行建模,可能是这样的:

protocol FillerProtocol {
    associatedtype ResultModel
}

class ListController<RequestModel,  Filler: FillerProtocol, Cell>: BaseViewController
    where
        Cell: BaseCell<Filler>,
        RequestModel: ExtendableByBaseListRequestModel
{

}

这样你甚至不必指定你的 ResultModel 类型,它由你的 FillerProtocol 定义。