Expression type is ambiguous without additional Swift context

I get the β€œ Type of expression ambiguous without context ” in this part of the code from the project that I am trying to upgrade to the latest version of Swift. I don't seem to understand. I tried different things, but I can not get it to work.

The problem is the syntax of this line

let imageToDeleteParameters = imagesToDelete.map { ["id": $0.id, "url": $0.url.absoluteString, "_destroy": true] } 

Full code:

 extension TutorialCreationRequest: WebserviceParametrable { func toParameters() -> [String: AnyObject] { let imageParameters = images.map { ["url": $0] } let imageToDeleteParameters = imagesToDelete.map { ["id": $0.id, "url": $0.url.absoluteString, "_destroy": true] } return [ "title": title, "is_draft": isDraft, "difficulty": difficulty, "duration": duration, "cost": cost, "user_id": userId, "description": description, "to_sell": toSell, "images": [imageParameters, imageToDeleteParameters].flatMap { $0 } ] } } 
+19
source share
6 answers

Not the answer to this question, but since I came here looking for an error, others may find this also useful:

For me, I got this Swift error when I tried to use a for (index, object) loop for (index, object) array without adding the .enumerated() ...

+12
source

The compiler cannot figure out what type of dictionary is because it is not homogeneous. You have different types of values. The only way around this is to make it [String: Any] , which will make everything awkward, like all hell.

 return [ "title": title, "is_draft": isDraft, "difficulty": difficulty, "duration": duration, "cost": cost, "user_id": userId, "description": description, "to_sell": toSell, "images": [imageParameters, imageToDeleteParameters].flatMap { $0 } ] as [String: Any] 

This is a job for the structure. This will greatly simplify the work with this data structure.

+10
source

This happens when you have a function with invalid argument names.

Example:

 functionWithArguments(argumentNameWrong: , argumentName2: ) 

and you declared your function as:

 functionWithArguments(argumentName1: , argumentName2: ){} 

This usually happens when you change the name of a variable. Make sure you refactor when you do this.

+9
source

An explicit input declaration for this display function should do the trick:

 let imageToDeleteParameters = imagesToDelete.map { (whatever : WhateverClass) -> Dictionary<String, Any> in ["id": whatever.id, "url": whatever.url.absoluteString, "_destroy": true] } 

Substitute the real class "$ 0" for "WhateverClass" in this piece of code, and it should work.

+1
source

I got this error when I put a space before the comma in the parameters when calling the function.

For example, I used:

 myfunction(parameter1: parameter1 , parameter2: parameter2) 

Whereas it should have been:

 myfunction(parameter1: parameter1, parameter2: parameter2) 

Removing the space eliminated the error message

+1
source

I had this message when the function parameter type did not fit. In my case, it was a string instead of a URL.

0
source

Source: https://habr.com/ru/post/1260600/


All Articles