Checking generic type in Swift

twitter logo github logo ・1 min read

Original post https://github.com/onmyway133/blog/issues/74

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 })
}
twitter logo DISCUSS
Classic DEV Post from Mar 13

Mozilla launches Firefox Send (free encrypted file transfer)

A thread to discuss Firefox Send.

Khoa Pham profile image
My apps https://onmyway133.github.io/