I need to get the generic type of an array.
I have an object that is an Array<Decodable>
, but I am not guaranteed that the generic type is always Decodable
. I know I can get the type of the thing by saying array.self.dynamicType
to get the Array.Type
, but I need something like array.generic.self.dynamicType
to get Decodable.Type
. How would I do this?
You can extend Array
with a computed property that exposes its generic type parameter Element
:
extension Array {
var ElementType: Element.Type {
return Element.self
}
}
print([1, 2, 3].dynamicType) //Array<Int>
print([1, 2, 3].ElementType) //Int