【问题标题】:How to append values to a dictionary after a firebase query?如何在 firebase 查询后将值附加到字典?
【发布时间】:2019-11-29 18:20:18
【问题描述】:

在下面的代码中,我试图将字符串 uids 附加到字典中,问题是当我这样做时,似乎字典在每次迭代后都会被重置。我该如何解决这个问题?

    func getFollowing() {
    self.ref.child("Following").child(uid)
        .observeSingleEvent(of: .value, with: { (snapshot) in

            if let data = snapshot.value as? [String:[String : Any]] {
                for key in data.keys {
                    var dict = (UIApplication.shared.delegate as! AppDelegate).usersCurrentUserIsFollowing
                    dict[key] = true
                    print(dict, " this is gtteh dict now dhfsakflk")
                }
            }
        })
}

【问题讨论】:

    标签: ios swift firebase dictionary firebase-realtime-database


    【解决方案1】:

    我能够通过执行以下操作来解决我的问题:

        func getFollowing() {
        self.ref.child("Following").child(uid)
            .observeSingleEvent(of: .value, with: { (snapshot) in
                var usersData = ["":true]
                if let data = snapshot.value as? [String:[String : Any]] {
                    for key in data.keys {
                        usersData[key] = true
                    }
                    var dict = (UIApplication.shared.delegate as! AppDelegate).usersCurrentUserIsFollowing
                    dict = usersData
                }
            })
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-17
      • 2013-08-19
      • 2013-01-09
      • 2016-12-30
      • 1970-01-01
      • 2019-08-28
      • 2020-04-09
      • 1970-01-01
      相关资源
      最近更新 更多