简体   繁体   中英

Alamofire: Cannot call value of non-function type 'NSHTTPURLResponse?'

I have an extension on a class called APIManager:

import Alamofire
import Foundation

// MARK: - Logout
extension APIManager {

    public func performLogout() {
        let request = defaultManager.request(APIManager.Router.Logout.URLRequest)

        request.response( // ERROR ON THIS LINE AS NOTED BELOW
            queue: APIManager.responseQueue,
            responseSerializer: Request.stringResponseSerializer(),
            completionHandler: { [weak self] (response)  in
                guard let strongSelf = self else { return }

            }
        )
    }

}

When the extension is included underneath the APIManager class in the SAME FILE, there is no issue whatsoever.

However, when I take the extension and move it into a NEW FILE, I receive this error:

Cannot call value of non-function type 'NSHTTPURLResponse?'

It's obvious that when the extension is placed in another file, the compiler is confusing the method above as defined Alamofire/ResponseSerialization.swift with the 'response' property in Alamofire/Request.swift of the Alamofire Request class

The APIManager class is below:

public final class APIManager {

  // MARK: - Singleton shared instance
  static let sharedInstance = APIManager()

  let defaultManager: Alamofire.Manager = {
    let serverTrustPolicies: [String: ServerTrustPolicy] = [

        "TBD1": .PinCertificates(
            certificates: ServerTrustPolicy.certificatesInBundle(),
            validateCertificateChain: true,
            validateHost: true
        ),

        "TBD2": .DisableEvaluation
    ]

    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.HTTPAdditionalHeaders = Alamofire.Manager.defaultHTTPHeaders

    return Alamofire.Manager(
        configuration: configuration,
        serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
    )
  }()
}

There is a similar question here but it's not quite the same:

Cannot call value of non-function type 'NSHTTPURLResponse?' Alamofire ObjectMapper

If this is, as you say, working fine in the other file a couple of suggestions come to mind.

  • Mark the class you are extending as public. (Swift classes are internal by default.)
  • Open the document inspector pane on the right of your class, make sure your target membership includes the target you are compiling for.

Let me know if your issue still persists and I will investigate further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM