在Swift中从Firebase检索数据的问题

时间:2017-07-02 00:17:16

标签: ios firebase swift3 firebase-realtime-database

我在Firebase中设置了一些数据对象,我试图在Wwift中检索它们并在表格视图中显示它们但我无法得到任何结果。

我尝试了什么?

var crystalsList = [Crystal]()

override func viewDidLoad() {
    super.viewDidLoad()
    print(crystalsList.count)
    crystalRef = Database.database().reference().child("crystalList")

    crystalRef?.observe(DataEventType.value, with: { (snapshot) in
        if snapshot.childrenCount > 0 {
            self.crystalsList.removeAll()

            for myCrystal in snapshot.children.allObjects as! [DataSnapshot]{
                let crystalObject = myCrystal.value as? [String : AnyObject]
                let name = crystalObject?["name"]
                let colors = crystalObject?["colors"]
                let mainColor = crystalObject?["mainColor"]
                let region = crystalObject?["region"]
                let rarity = crystalObject?["rarity"]
                let additionalInfo = crystalObject?["additionalInfo"]
                let primaryUse = crystalObject?["primaryUse"]
                let isNew = crystalObject?["isNew"]

                let theCrystal = Crystal(name: (name as! String?)!, rarity: (rarity as! String?)!, region: (region as! String?)!, color: (colors as! String?)!, mainColor: (mainColor as! String?)!, isNewListing: (isNew as! Bool), additionalInfo: (additionalInfo as! String?)!, primaryUse: (primaryUse as! String?)!)

                self.crystalsList.append(theCrystal)
            }

            self.crystalTable.reloadData()
        }
    })

}

1 个答案:

答案 0 :(得分:0)

感谢frank,我能够通过进入firebase并允许读取数据来解决此问题。