使用Firebase Dictionary创建数组

时间:2017-10-11 18:53:22

标签: arrays swift firebase firebase-realtime-database

我正在尝试从Firebase检索字典并从字典中提取每个值并将其附加到空数组,但我的代码不起作用。我甚至没有添加代码将它附加到数组中,当我运行它时,"错误"在控制台中打印。

This is what it looks like inside Firebase

这就是我的代码:

    func convertAllMosaicsToArray() {
    // retrieve and convert here

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

        if let dictionary = snapshot.value as? [Int : AnyObject] {
            print(dictionary)
        } else {
            print("error")
        }
    })

}

2 个答案:

答案 0 :(得分:0)

select * from test_table where id=123

答案 1 :(得分:0)

问题是使用if let语句将其转换为[Int:AnyObject],只需将其更改为[String]

像这样:

    func retrieveMosaicTitles() {
    // retrieve and convert here

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

        if let allMosaicTitles = snapshot.value as? [String] {
            self.searchResultsVC.listOfMosaics = allMosaicTitles
        } else {
            print("error")
        }
    })
}
相关问题