ppshein ppshein - 1 month ago 29
Swift Question

Type of expression is ambiguous without more context in Alamofire.upload swift 3

Updated Alamofire 4.0.0 does not mention how to put Httpmethod & Httpheaders in upload with multipartFormData. That's why I google and found solution in that stackoverflow question. But the problem is I did same as that answer then got following error message and building is failed. Please help me how to solve it.


Type of expression is ambiguous without more context


Here is my coding:

let URL = try! URLRequest(url: Config.imageUploadURL, method: .post, headers: headers)

Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(self.imageData, withName: "image", fileName: "file.png", mimeType: "image/png")
},
to: URL,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
if((response.result.value) != nil) {

} else {

}
}
case .failure( _):

}
}
)

Answer

Alamofire.upload(multipartFormData:to:encodingCompletion:) takes a URLConvertible for the to: argument. Instead, you should use Alamofire.upload(multipartFormData:with:encodingCompletion:) which takes a URLRequestConvertible for its with: argument.

I think your argument name of URL that is the same as the type URL() helps in creating strange compiler errors.

The following compiles for me:

let url = try! URLRequest(url: URL(string:"www.google.com")!, method: .post, headers: nil)

Alamofire.upload(
    multipartFormData: { multipartFormData in
        multipartFormData.append(Data(), withName: "image", fileName: "file.png", mimeType: "image/png")
    },
    with: url,
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                if((response.result.value) != nil) {

                } else {

                }
            }
        case .failure( _):
            break
        }
    }
)