如何在Swift中将Firebase数据库快照转换为数组列表?

时间:2018-08-12 17:04:47

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

我正在使用Swift和Firebase制作一个应用程序。我想获得一个数组列表,其中包含某个用户ID下的所有数字(下面附有图像)。我的意思是,我希望能够调用一个函数,该函数返回一个数组列表,该数组列表包含作为用户ID的子代放置的每个整数(从低到高),但不包含其值(在这种情况下为“ true” )。我已经获得了数据的快照(请参见下面的代码),但是我不确定现在该怎么做。

我的代码:

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()
        }
    })
}

该函数打印: enter image description here

这是实时数据库的图像: enter image description here

1 个答案:

答案 0 :(得分:0)

通过键创建数组

let array = Array(dictionary.keys)