Woodyhang
Woodyhang

Reputation: 33

type(of :) function compiler warning in Swift 4

I have upgraded the project from Swift 3 to Swift 4, but the compiler suggested that the conditions in the if can not call value of non-function type 'AnyClass' (aka 'AnyObject.Type'). How to solve this?

func getHandler(_ type:AnyClass) -> CSHandler? {
    for handler in handlers {
        if type(of: handler) === type {
            return handler
        }

    }

    return nil
}

Upvotes: 2

Views: 98

Answers (1)

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

I think compiler is getting confused with the parameter type and method type(of:) both have the same initial token. Can you change the parameter name from type to pType as below

func getHandler(_ pType: AnyClass) -> AnyObject? {
  for handler in handlers {
    if type(of: handler) === pType {
      return handler
    }
  }
  return nil
}

This should work for you and I would suggest you to file a bug report to apple regarding this issue

Upvotes: 1

Related Questions