【发布时间】:2019-06-25 17:13:59
【问题描述】:
我想创建一个接受协议类型参数的函数,并确保该协议类型也符合另一种协议类型。有没有办法我可以做到这一点?还是我必须从根本上重新考虑我的方法?
例子:
// This doesn't extend CaseIterable itself because I would like to use it as a concrete type and not just a generic constraint
protocol MyProtocol {
/*some protocol stuff*/
}
enum myEnum: MyProtocol, CaseIterable {
/*some enum stuff*/
}
func<T: CaseIterable>(_ myEnum: MyProtocol.Type)
where MyProtocol.Type: CaseIterable {
myEnum.allCases // <--- This is what I would like to do
}
【问题讨论】:
标签: swift generics enums swift-protocols