Scala:在带有嵌套Map的Map上使用toMap失败(Scala: using toMap on a Map with a nested Map fails)
在我的代码中,我有以下Map定义了嵌套映射,我想使用函数
getTags
获取Map的这个值(嵌套Map),这会产生以下错误:scala> def getTags(tags: Map[String, Map[String, List[String]]]): Map[String, List[String]] = { | tags.map {_._2}.toMap | } <console>:12: error: Cannot prove that Map[String,List[String]] <:< (T, U). tags.map {_._2}.toMap ^ ^
我已经确认在使用
toMap
函数之前,我得到的错误是:scala> def getTags(tags: Map[String, Map[String, List[String]]]): Map[String, List[String]] = { | tags.map {_._2} | } <console>:12: error: type mismatch; found : scala.collection.immutable.Iterable[Map[String,List[String]]] required: Map[String,List[String]] tags.map {_._2} ^
所以在
toMap
之前返回的类型是Iterable[Map[String, List[String]]]
。我怎样才能得到
Map[String, List[String]]
?感谢有关此问题的任何帮助。
In my code, I have the following Map with a nested map defined, I want to fetch this value of the Map (the nested Map) using a function
getTags
which produces the following error:scala> def getTags(tags: Map[String, Map[String, List[String]]]): Map[String, List[String]] = { | tags.map {_._2}.toMap | } <console>:12: error: Cannot prove that Map[String,List[String]] <:< (T, U). tags.map {_._2}.toMap ^ ^
I've confirmed that before using the
toMap
function, the error I get is:scala> def getTags(tags: Map[String, Map[String, List[String]]]): Map[String, List[String]] = { | tags.map {_._2} | } <console>:12: error: type mismatch; found : scala.collection.immutable.Iterable[Map[String,List[String]]] required: Map[String,List[String]] tags.map {_._2} ^
So the type returned before
toMap
isIterable[Map[String, List[String]]]
.How can I get a hold of the
Map[String, List[String]]
?Appreciate any help on the issue.
原文:https://stackoverflow.com/questions/47143891
最满意答案
您必须记住,
observeSingleEvent
是异步触发的,因此将它放入Dispatch组可能是个好主意。我建议你做的是从一个组内执行
loadAllDataFromServerAboutGame
方法,并且在完成触发之前不做任何事情,并且所有数据都已写入变量。你在哪里调用loadAllDataFromServerAboutGame
let group = DispatchGroup() group.enter() loadAllDataFromServerAboutGame() group.notify(queue: .main, execute: { // Do something }
你的
loadAllDataFromServerAboutGame
方法:func loadAllDataFromServerAboutGame(){ ref.child("\(gamePIN)").observeSingleEvent(of: .value, with: { (snapshot) in // Get user value let value = snapshot.value as? NSDictionary self.player1Name = value?["player1Name"] as? String ?? "" self.player2Name = value?["player2Name"] as? String ?? "" self.player3Name = value?["player3Name"] as? String ?? "" self.player4Name = value?["player4Name"] as? String ?? "" self.player5Name = value?["player5Name"] as? String ?? "" self.player6Name = value?["player6Name"] as? String ?? "" self.playerAmountBeforeTextChanging = value?["numberOfPlayers"] as? String ?? "" self.player1UID = value?["player1UID"] as? String ?? "" self.player2UID = value?["player2UID"] as? String ?? "" self.player3UID = value?["player3UID"] as? String ?? "" self.player4UID = value?["player4UID"] as? String ?? "" self.player5UID = value?["player5UID"] as? String ?? "" self.player6UID = value?["player6UID"] as? String ?? "" if let myNumber = NumberFormatter().number(from: self.playerAmountBeforeTextChanging) { self.numberOFPLayers = myNumber.intValue } else { print("Error converting text into 'int' value!") } print("Transcription: \(self.numberOFPLayers)") print("Player 3 Name: \(self.player3Name)") //HERE: group.leave() // ... }) { (error) in print("DataBase ERROR:\(error.localizedDescription)") //HERE: group.leave() } }
You have to remember that the
observeSingleEvent
is fired asynchronous, and therefore it might be a good idea to put it into a Dispatch Group.What I would suggest you do, is the to execute the method
loadAllDataFromServerAboutGame
from within a group, and not do anything until after it has finished firing, and all data has been written to the variables.Where you call loadAllDataFromServerAboutGame
let group = DispatchGroup() group.enter() loadAllDataFromServerAboutGame() group.notify(queue: .main, execute: { // Do something }
Your
loadAllDataFromServerAboutGame
method:func loadAllDataFromServerAboutGame(){ ref.child("\(gamePIN)").observeSingleEvent(of: .value, with: { (snapshot) in // Get user value let value = snapshot.value as? NSDictionary self.player1Name = value?["player1Name"] as? String ?? "" self.player2Name = value?["player2Name"] as? String ?? "" self.player3Name = value?["player3Name"] as? String ?? "" self.player4Name = value?["player4Name"] as? String ?? "" self.player5Name = value?["player5Name"] as? String ?? "" self.player6Name = value?["player6Name"] as? String ?? "" self.playerAmountBeforeTextChanging = value?["numberOfPlayers"] as? String ?? "" self.player1UID = value?["player1UID"] as? String ?? "" self.player2UID = value?["player2UID"] as? String ?? "" self.player3UID = value?["player3UID"] as? String ?? "" self.player4UID = value?["player4UID"] as? String ?? "" self.player5UID = value?["player5UID"] as? String ?? "" self.player6UID = value?["player6UID"] as? String ?? "" if let myNumber = NumberFormatter().number(from: self.playerAmountBeforeTextChanging) { self.numberOFPLayers = myNumber.intValue } else { print("Error converting text into 'int' value!") } print("Transcription: \(self.numberOFPLayers)") print("Player 3 Name: \(self.player3Name)") //HERE: group.leave() // ... }) { (error) in print("DataBase ERROR:\(error.localizedDescription)") //HERE: group.leave() } }
相关问答
更多-
尝试在列表单元格中编辑它 var imageURL = String() func retrieveImages() { let decodedData = NSData(base64EncodedString: imageURL, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters) let decodedImage = UIImage(data: decodedData!) self.headImg.image = decode ...
-
在这行代码中: usernamerecieved = globalusername你在这一行中所做的是你将usernamerecieved的值设置为globalusername的值,即'user'它应该是globalusername = usernamerecieved 。 有时从服务器检索数据需要花费时间的原因可能很多 - 网络连接速度慢,数据检索繁重,如图像等,但在它完成之前(从服务器检索动作)可能是你在哪一行正在打印globalusername被调用,甚至在完成后仍然关闭observeElock的ob ...
-
您通常希望在Swift代码和服务器端安全规则中添加验证,但原因各不相同。 您必须在服务器上进行验证,因为它是恶意客户端无法绕过的唯一验证类型。 此验证是您防止错误数据的最后一道防线。 您可能还想验证客户端上的数据。 这更像是为您的用户提供的服务:如果您在客户端上进行验证,则可以根据您的规则向他们提供有关其数据是否有效的早期(通常更好)消息。 You'll typically want to add validation both in your Swift code and in your server-s ...
-
您必须记住, observeSingleEvent是异步触发的,因此将它放入Dispatch组可能是个好主意。 我建议你做的是从一个组内执行loadAllDataFromServerAboutGame方法,并且在完成触发之前不做任何事情,并且所有数据都已写入变量。 你在哪里调用loadAllDataFromServerAboutGame let group = DispatchGroup() group.enter() loadAllDataFromServerAboutGame() group.noti ...
-
Firebase搭配Swift(Firebase with Swift)[2022-04-02]
我设法让它运作起来。 事实证明,firebase网站上的视频解释了哪些依赖项已经过时了。 我在页面下面进一步阅读,看到firebase实际上需要两个我缺少的依赖项。 添加libc++.dylib , libiucore.dylib , CFNetwork.framework , Security.framework和SystemConfiguration.framework一切正常。 I managed to get it working. It turns out that the video on th ... -
假设您的节点“currentGame”中有多个对象,并且您希望从所有这些对象中提取玩家地址坐标和当前游戏ID密钥,请按以下步骤操作: self.ref.child("users").child(userFound.userRef!).child("currentGame").observeSingleEvent(of: .value, with: { (snapshot) in if(snapshot.exists()) { let enumerato ...
-
为了实现您的目标,您通常希望拥有存储在数据库中的咖啡馆的确切位置,即(纬度,经度) 。 所以你的场地结构看起来就像这样 "venues" : { "CrokePark" : { "latitude": 37.335556, // this is a double "longitude": -122.009167 // this is a double "beverages" : { "drink1" : "Coca-Cola", "drink2" : "Fanta", " ...
-
希望这会提供一些方向,请原谅。 蝙蝠的权利,通常有更好的方法来存储Firebase中的数据而不是数组; 数组不可搜索,可编辑或可添加,如果你想改变整个数组需要被覆盖。 请参阅Arrays Are Evil 。 其次,Firebase中的非规范化数据是正常的。 这意味着当数据太深时,查询所需数据就困难得多。 通常更平坦更好 - 取决于你想要从Firebase中获得什么。 第三项是有100种不同的方式处理你的问题中的数据,我会提出两个; 一个在概念上展示如何写数据,第二个可能是一个更好的选择。 以下是如何以暴力 ...
-
如果所有键都是整数,并且对象中超过一半的键与对象中的最大键具有非空值,则Firebase会将其渲染为数组。 所以我相信你总是使用一些字符串作为键而不是Int,因为顺序方式键经常转换为数组,因为firebase认为它的自动递增... 更多信息。 检查这个答案 If all of the keys are integers, and more than half of the keys between 0 and the maximum key in the object have non-empty valu ...
-
这是一个非常模糊的问题,有很多答案,但这是基础知识。 假设你有这样的Firebase结构 users uid_0 user_name: "Bill" email: "bill@yipee.com" 要将用户名从Bill更改为Leeeeroy: var userNameRef = ref.childByAppendingPath("users/uid_0/user_name") userNameRef.setValue("Leeeeroy") 以上是一个小例子。 您还应该使用.upda ...