Nikita Ermolenko Nikita Ermolenko - 1 year ago 108
iOS Question

Type of Generic

Can I somehow check a type of

. I want to check that this type is
or something else.

func request<T: Mappable>(completionHandler: (Result<T>) -> Void) {


Answer Source

One way to do this is to overload the method, like so:

private func request<T: Mappable>(isArray: Bool, completionHandler: (Result<T>) -> Void) {
  if isArray {
    print("is array")
  } else {
    print("is not array")

func request<T>(completionHandler: (Result<Array<T>>) -> Void) {
  request(isArray: true, completionHandler: completionHandler)

func request<T: Mappable>(completionHandler: (Result<T>) -> Void) {
  request(isArray: false, completionHandler: completionHandler)

I'm assuming that somewhere you have defined:

extension Array: Mappable {}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download