Swift4,Firebase-仅检索key的值并放入数组

时间:2018-10-17 09:10:19

标签: arrays swift firebase firebase-realtime-database key-value

我想从Firebase检索'itemname'的所有值,并将它们放在我的数组generateObjects中。现在,当我只想要'itemname'('Toiletry bag','Toothbrush'等)的所有值时,好像我得到了父级,还有它的子级的键和值。不确定从这里去哪里,请提供帮助:

enter image description here

enter image description here

我的代码:

self.ref.child("lists").child("-LOXr5PoUvBn_tGNhql-").child(whatList!).observeSingleEvent(of: .value, with: { (snapshot) in
            let children = snapshot.children
            while let rest = children.nextObject() as? DataSnapshot{
                print(rest.value)
                //self.generatedObjects.append(rest.value as! [String: AnyObject])
            }

        },withCancel: nil)

2 个答案:

答案 0 :(得分:1)

尝试一下

func getData() {
   ref.child("lists").child("-LOXr5PoUvBn_tGNhql-").child("mylist").observe(.childAdded) { (snapshot) in
        let result = snapshot.value as? [String: Any]

        let item = result!["itemname"]
        //append item to your array
        ...
    }
}

答案 1 :(得分:0)

func fetchdata(toId: String)
{
    Database.database().reference().child("Users").child(toId).observeSingleEvent(of: .value) { (snapshot) in
        guard let dictionary = snapshot.value as? [String:Any] else {return}
        let itemname = dictionary["itemname"] as! String

        print(itemname)

    }
}

对您有帮助!

相关问题