"Ambiguous reference to a map of members" when trying to add / replace an array element

Several SO messages like this relate to the same error message, but none of these solutions work. It looks like this might be the case of an erroneous error message.

The code below generates the "Minor membership card link" error to call the card.

Does anyone know why?

func saveUser(user: User) { var userDicts = masterDict["users"] as! [[String:AnyObject]] let newDict = user.getDict() // Replace matching element with <newDict> let replaced = false masterDict["users"] = userDicts.map { if ($0["id"] as String! == user.getId()) { replaced = true return newDict } else { return $0 as [String:AnyObject] } } // If no match found, means must add new user if (!replaced) { userDicts.append(newDict) } } 
+5
source share
2 answers

Unfortunately, fast is not perfect and cannot call types at all times, so you get an ambiguous reference. Try userDicts.map {val -> [String:AnyObject] in and replace $0 with val . I will explicitly tell the map that the values ​​included in the number are [String:AnyObject] and should be able to return the specified type

+20
source

Never highlight the cause of the error, so switch to using the for loop, create a new array inside the loop, and call masterDict["users"] = newArray after the loop. Not the cleanest, but it fixes the error.

0
source

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


All Articles