简体   繁体   English

如何包含Firebase快照键值? [迅速]

[英]How to include firebase snapshot key value? [Swift]

Please anyone who would show me how to include the snapshot key value along with children value that I already append to my array (forgot to include it and now remember I have to) ... aside from that something really I couldn't understand when I tried to solve my own issue, by testing the value first by using this method: print(rooms.popFirst().key!) half of my database values got nil value ?!! 请任何愿意向我展示如何将快照键值以及我已经附加到数组中的子代值包括在内的人(忘记包括它,现在记住我必须这样做)……除此之外,我真的不明白什么时候我试图通过使用以下方法首先测试该值来解决自己的问题: print(rooms.popFirst().key!)我一半的数据库值都为nil值?! and if I don't include that method everything works fine anyways if you can't imagine that nonetheless I really wish your suggestion or advice for getting all data with their own key value... This is my code so far: 如果我不包含该方法,那么即使您无法想象仍然很不错,但我还是希望您的建议或建议能够获取所有具有其自身键值的数据...到目前为止,这是我的代码:

Database.database().reference().child("rooms").observe(.value, with: { (snapshot) in
        print()

        var rooms = snapshot.value as! [String:AnyObject]

        for(_,value) in rooms  {

            if (rooms.popFirst()?.key) != nil{

                let title = value["title"] as? String
                let description = value["description"] as? String
                let roomPictureUrl = value["Room Picture"] as? String
                let longitude = value["Longtitude"] as? String
                let latitude = value["Latitude"] as? String
                let dateFrom = value["Date From"] as? String
                let dateTo = value["Date To"] as? String
                let owner = value["Owner"] as? String

                let myRooms = Room(roomID: "XXX",title: title!, description: description!, roomPicutreURL: roomPictureUrl!, longitude: longitude!, latitude: latitude!, dateFrom: dateFrom!, dateTo: dateTo!, owner: owner!)

                //print(rooms.popFirst()?.key)
                self.rooms.append(myRooms)
                self.tableView.reloadData()



            }
        }
    })

Try this: 尝试这个:

Database.database().reference().child("rooms").observe(.value, with: { (snapshot) in

    var rooms = snapshot.value as! [String:AnyObject]
    let roomKeys = Array(rooms.keys)

    for roomKey in roomKeys  {

            guard

                let value = rooms[roomKey] as? [String:AnyObject]

            else
            {
                 continue
            }

            let title = value["title"] as? String
            let description = value["description"] as? String
            let roomPictureUrl = value["Room Picture"] as? String
            let longitude = value["Longtitude"] as? String
            let latitude = value["Latitude"] as? String
            let dateFrom = value["Date From"] as? String
            let dateTo = value["Date To"] as? String
            let owner = value["Owner"] as? String

            let myRooms = Room(roomID: "XXX",title: title!, description: description!, roomPicutreURL: roomPictureUrl!, longitude: longitude!, latitude: latitude!, dateFrom: dateFrom!, dateTo: dateTo!, owner: owner!)

            print(roomKey)
            self.rooms.append(myRooms)
            self.tableView.reloadData()



        }
    }
})

To get the key from snapshot you can use following lines of code it will give you result like -> Kqwewsds12 -> your child Details 要从快照中获取密钥,您可以使用以下代码行,它将得到如下结果: -> Kqwewsds12 -> your child Details

let dictValues = [String](snapshot.keys)

            print(dictValues[0]) //Output -- Kqwewsds12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM