How to check generic type in Swift

Issue #74

When dealing with generic, you shouldn’t care about the types. But if you need, you can

1
2
3
4
5
6
7
8
9
10
11
 func isPrimitive<T>(type: T.Type) -> Bool {
let primitives: [Any.Type] = [
Bool.self, [Bool].self,
String.self, [String].self,
Int.self, [Int].self,
Float.self, [Float].self,
Double.self, [Double].self
]

return primitives.contains(where: { $0.self == type.self })
}

Comments