简体   繁体   中英

How can I convert a Firebase Database Snapshot into an Array List in Swift?

I am making an app using Swift and Firebase. I want to get an array list containing all of the numbers under a certain user id (images attached below). What I mean by this is I want to be able to call a function that returns an array list containing every integer (from low to high) placed as a child of the user id, but not containing their values (in this case "true"). I have already gotten a snapshot of the data (see code below), but I am unsure as to what to do now.

My Code:

func likeToLikeForAll() {

    let uid  = Auth.auth().currentUser?.uid
    Database.database().reference().child("Liked Movies").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] {
            print()
            print("SNAP:")
            print(snapshot.value!)
            print()
        }
    })
}

The function prints: 在此处输入图片说明

This is an image of the realtime database: 在此处输入图片说明

从键创建数组

let array = Array(dictionary.keys)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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