[英]Swift Get Generic Type of Array
我需要获取数组的通用类型。
我有一个对象,它是Array<Decodable>
,但是不能保证泛型类型始终为Decodable
。 我知道我可以说得到的东西类型array.self.dynamicType
得到Array.Type
,但我需要像array.generic.self.dynamicType
得到Decodable.Type
。 我该怎么做?
您可以使用显示其通用类型参数Element
的计算属性扩展Array
:
extension Array {
var ElementType: Element.Type {
return Element.self
}
}
print([1, 2, 3].dynamicType) //Array<Int>
print([1, 2, 3].ElementType) //Int
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.