首页 \ 问答 \ Scala:在带有嵌套Map的Map上使用toMap失败(Scala: using toMap on a Map with a nested Map fails)

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 is Iterable[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
更新时间:2023-12-03 08:12

最满意答案

您必须记住, 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 observeSingleEventis 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网站上的视频解释了哪些依赖项已经过时了。 我在页面下面进一步阅读,看到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 ...

相关文章

更多

最新问答

更多
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • 电脑高中毕业学习去哪里培训
  • 电脑系统专业就业状况如何啊?
  • IEnumerable linq表达式(IEnumerable linq expressions)
  • 如何在Spring测试中连接依赖关系(How to wire dependencies in Spring tests)
  • Solr可以在没有Lucene的情况下运行吗?(Can Solr run without Lucene?)
  • 如何保证Task在当前线程上同步运行?(How to guarantee that a Task runs synchronously on the current thread?)
  • 在保持每列的类的同时向数据框添加行(Adding row to data frame while maintaining the class of each column)
  • 的?(The ? marks in emacs/haskell and ghc mode)
  • 一个线程可以调用SuspendThread传递自己的线程ID吗?(Can a thread call SuspendThread passing its own thread ID?)
  • 延迟socket.io响应,并“警告 - websocket连接无效”(Delayed socket.io response, and “warn - websocket connection invalid”)
  • 悬停时的图像转换(Image transition on hover)
  • IIS 7.5仅显示homecontroller(IIS 7.5 only shows homecontroller)
  • 没有JavaScript的复选框“关闭”值(Checkbox 'off' value without JavaScript)
  • java分布式框架有哪些
  • Python:填写表单并点击按钮确认[关闭](Python: fill out a form and confirm with a button click [closed])
  • PHP将文件链接到根文件目录(PHP Linking Files to Root File Directory)
  • 我如何删除ListView中的项目?(How I can remove a item in my ListView?)
  • 您是否必须为TFS(云)中的每个BUG创建一个TASK以跟踪时间?(Do you have to create a TASK for every BUG in TFS (Cloud) to track time?)
  • typoscript TMENU ATagParams小写(typoscript TMENU ATagParams lowercase)
  • 武陟会计培训类的学校哪个好点?
  • 从链接中删除文本修饰(Remove text decoration from links)