Search code examples
iosswiftresponsealamofireambiguous

Expression type 'DataRequest' is ambiguous without more context Swift


I use Alamofire for my request in this function and I have this error if someone could help me please thank in advance.

Expression type 'DataRequest' is ambiguous without more context

 func report(_ track: Track, completionHandler: @escaping (_ error: NSError?) -> Void) {
    var headers:[String:String] = [:]
    if AuthManager.defaultAuthManager().isLoggedIn() {
        headers = ["Authorization": "Bearer " + AuthManager.defaultAuthManager().authToken.token!]
    }
    let params: [String: Any] = ["trackCode": track.code]
    let urlString = Cizoo.APIBaseUrl + CizooScheme.report
    CizooAPI.manager.request(urlString, method: .post, parameters: params, encoding: .JSONEncoding.default, headers: headers as HTTPHeaders)
        .validate()
        .responseJSON(completionHandler: { // Error at this line
            response in
            switch response.result {
            case .success:
                completionHandler(error: nil)
            case .failure(let error):
                completionHandler(error: error)
            }
        })
}

Solution

  • From the Alamofire source code you can see that the declaration of the method is:

    @discardableResult
    open func request(
        _ url: URLConvertible,
        method: HTTPMethod = .get,
        parameters: Parameters? = nil,
        encoding: ParameterEncoding = URLEncoding.default,
        headers: HTTPHeaders? = nil)
        -> DataRequest
    {
        var originalRequest: URLRequest?
    
        do {
            originalRequest = try URLRequest(url: url, method: method, headers: headers)
            let encodedURLRequest = try encoding.encode(originalRequest!, with: parameters)
            return request(encodedURLRequest)
        } catch {
            return request(originalRequest, failedWith: error)
        }
    }
    

    Which in your case probably the urlString is not conforming to the URLConvertible protocol.