error solved

Solved – type of expression is ambiguous without more context


In this article, you will learn how to solve type of expression is ambiguous without more context.

Let’s look at a code example that produces the same error.

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( _):

        }
    }
)

“expression is ambiguous without more context” error means that the compiler is unable to understand what you’re trying to express because you aren’t being specific enough.

In order to solve it, you have put correct arguments. In our case we have to use Alamofire.upload(multipartFormData:with:encodingCompletion:) which takes a URLRequestConvertible for its with: argument.

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
        }
    }
)

Consider the another code snippet below which produces same error.

....
multipartFormData: { multipartFormData in
        multipartFormData.append(data: imageData!, withName: "unicorn")
    },
....

In the code snippet above we have the wrong arguments for multipartFormDatawhich can be easily fixed like below:

...
multipartFormData: { multipartFormData in
                    multipartFormData.append(imageData!, withName: "photo", fileName: "image.jpg", mimeType: "image/jpg")
                },
...


Share on social media

//