几个SO帖子
like this处理相同的错误消息,但这些解决方案都不起作用.看起来这可能是一个误导性错误消息的情况.
谁知道为什么?
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) } }
不幸的是,swift并不完美,无法在任何时候推断出类型,这就是为什么你得到了模糊的参考.试试userDicts.map {val – > [String:AnyObject] in并为$换出$0这将明确告诉地图进来的值是[String:AnyObject],并且应该能够返回所述类型