gpt4 book ai didi

ios - 如何在 Firebase 中将项目更新/添加到 JSON 数组?

转载 作者:行者123 更新时间:2023-11-28 08:18:54 25 4
gpt4 key购买 nike

我有以下 Firebase 数据结构:

"Group" : {
"34923493" : {
"Name" : "Group 1"
"Users" : {
joeshmoe : true
janedoe : true
}
etc.....
}
}

我想将以下内容添加到“用户”部分:

 johndoe : true

因为用户已经加入了群组。我有以下代码

self.ref.child("Group").child(groupID!).child("Users").setValue([user?.username : true])

但我收到以下错误:

"Contextual type'Any' cannot be used with dictionary literal"

我不确定我做错了什么?

最佳答案

您的代码中有两处错误。

  1. 您遇到的错误很可能是由于成员变量 username 的类型为 Any 引起的。所以,只需将其向下转换为 String

    [user?.username as! String: true]
  2. 这一行

    self.ref.child("Group").child(groupID!).child("Users").setValue([user?.username : true])

    将从字典中删除所有其他用户。您可能会将 [user?.username: true] 分配给那个 child ,删除已经存在的字典值。直接更新字典键,如下所示:

    self.ref.child("Group").child(groupID!).child("Users").child(user?.username as! String).setValue(true)

关于ios - 如何在 Firebase 中将项目更新/添加到 JSON 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41796941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com