DEV Community


Checking generic type in Swift

Khoa Pham
My apps
・1 min read

Original post

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

 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 })
Enter fullscreen mode Exit fullscreen mode

Discussion (0)