Firebase - how to get the key value in observeEventType = Value

后端 未结 3 833
礼貌的吻别
礼貌的吻别 2020-12-06 08:39

This is a follow up question to Firebase - proper way to structure the DB

I have the following DB structure:

\"artists\" : {
  \"-KKMkpA22PeoHtBAPyKm         


        
3条回答
  •  长情又很酷
    2020-12-06 09:10

    You can get the Keys with the help of Dictionary itself.
    
        Database.database().reference().child("artists").observe(.value, with: { (snapshot) in
            if snapshot.exists() {
                if let artistsDictionary = snapshot.value as? NSDictionary {
                    for artists in artistsDictionary.keyEnumerator() {
                        if let artistsKey = artists as? String {
                            print(artistsKey) // Here you will get the keys.
                        }
                    }
                }
            } else {
                print("no data")
            }
        }) { (error) in
            print(error)
        }
    

提交回复
热议问题