这不是 In Swift, how can I declare a variable of a specific type that conforms to one or more protocols? 的重复。该问题针对我需要元类型的具体用例,而且如何实现并不明显。
在Swift 4中,可以声明一个既是子类又符合多个协议的变量:
var myVariable: MyClass & MyProtocol & MySecondProtocol
我需要这样的一致性,但不是针对实例,而是针对类型本身。具体来说,是为了以下语法:
var classForCell: UICollectionViewCell.Type & AdditionalHeightable.Type
我在使用Swift 4时遇到了以下错误:
Non-protocol, non-class type 'UICollectionViewCell.Type' cannot be used within a protocol-constrained type
如何声明一个既是子类又符合协议的元类型?
无法将类型'(UIViewController&ExperienceDelegate).Type'分配给类型'ExperienceDelegate?'
有什么想法如何处理这个问题吗? - Hadi Sharghi(UIViewController & ExperienceDelegate).Type
,而是直接使用UIViewController & ExperienceDelegate
即可。 - Marián Černý