且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用泛型将符合协议的对象和泛型存储在类型数组中

更新时间:2022-05-16 21:44:45

您不能拥有类型为Adjustable的项目数组,因为Adjustable并不是真正的类型.这是一张蓝图,描述了一组类型,每个T的可能值中的一个.

You can't have an array of items of type Adjustable, because Adjustable isn't really a type. It's a blue print that describes a set of types, one per every possible value of T.

要解决此问题,您需要使用类型橡皮擦 https://medium.com/dunnhumby-data-science-engineering/swift-associated-type-design-patterns-6c56c5b0a73a

To get around this, you need to use a type eraser https://medium.com/dunnhumby-data-science-engineering/swift-associated-type-design-patterns-6c56c5b0a73a