Fix error

Solve – type of expression is ambiguous without more context in swift

type of expression is ambiguous without more context

The Swift error “type of expression is ambiguous without more context” occurs when you try to update your project to the latest version of Swift or XCode to 7.0.1+ from version 6.4. The compiler will throw this error letting you know that there’s something wrong somewhere in your code. Also, you have to be specific enough to help the compiler understand what you intend to express. This mainly happens when you declare a function that accepts argument and you passed in wrong argument name due to typo. Or if you change a function argument name without refactoring the new changes to reflect in all other files where it being used.

For example, we have declared a function in below code that accept two arguments. When calling it we made a typo in the arguments name and this will cause the above error. The good thing is that the compiler will give you a full hint about the error including the file and the line the error is located depending on the type of XCode you’re using.

functionWithTwoArguments(firstArgumentName: , secondArgumentName: ){}

functionWithTwoArguments(firstargumentNameWrong: , secondArgumentName: )

Let me show you another Swift code example that causes the swift-Compiler to produce 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) {
                  // we have some response
                } else {
                 // we have no response due to a bug
                }
            }
       // run this in case it fails
       // it will lead to an error because we have to break the code-execution 
        case .failure( _):

        }
    }
)

The above code will throw an error because we aren’t being specific about the expression we are trying to execute. What are we doing wrong actually? We are passing wrong arguments to the method and we have also not declared any URL. to solve it we have to pass in the correct one. The correct code to use is Alamofire.upload(multipartFormData:with:encodingCompletion:) and if you check the code above you can see that it takes URLRequestConvertible

// Now we are being clear about the url
let url = try! URLRequest(url: URL(string:"www.swift.com")!, method: .post, headers: nil)

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

                } else {

                }
            }
// run this in case it fails
// now we are breaking the code-execution if it failes
        case .failure( _):
            break
        }
    }
)

Check also, fix git error: failed to push some ref to remote origin

Below code snippet will also throw swift error – type of expression is ambiguous without more context.

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

Look at the code snippet above and check the wrong argument we have passed to the multipartFormData function. We have passed an incomplete argument and since the function requires more than the number of arguments we have provided, it will cause an error. To fix the above bug, all we need to do is to provide all the correct argument with the right data-type.

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

Alternative, the error might be related to swiftui widget.

var firstName = personalFisrtname.text
var lastName = personalLastname.text
var myEmail = myPersonalEmail.text

Check also, how to fix error – JSX expressions must have one parent element in React

If all the above three variables are object of UITextFields, then they will return Optional Strings. All the three properties will become…

var firstName: String? = personalFisrtname.text
var lastName: String? = personalLastname.text
var myEmail: String? = myPersonalEmail.text

But withArgs expect array of Strings and you have to check and pass in optional string value that UITextField will fall back on if the text property is nil.

var firstName = personalFisrtname.text ?? ""
var lastName = personalLastname.text ?? ""
var myEmail = myPersonalEmail.text ?? ""
Tagged

About Justice Ankomah

computer science certified technical instructor. Who is interested in sharing (Expert Advice Only)
View all posts by Justice Ankomah →

Leave a Reply

Your email address will not be published. Required fields are marked *