受约束的协议仍然抱怨通用

时间:2020-05-25 01:05:08

标签: ios swift generics protocols

因此,首先,我了解其中包含associatedtype的协议将变得通用,因此将它们用作属性毫无意义。但是问题是,为什么已经受到限制的协议仍属于“成为通用”性质?

例如:

protocol ItemCollection {
    associatedtype ItemType
    var items: [ItemType]
}

protocol StringItemCollectable: ItemCollection where ItemType == String {
    // other implementation
}

class Foo {
    // item model
    var itemCollectionModel: StringItemCollectable? // Compiler here is still complaining about using generic protocols

}

如果我创建一个符合StringItemCollectable的具体类型,则编译器将知道itemsString的数组。

struct TestCollectionModel: StringItemCollectable {
    var items: [String] // this would be required and already known from the compiler
}

为什么编译器会认为class Foo中受约束的协议仍然是通用的?

0 个答案:

没有答案