我在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()
}
})
}
答案 0 :(得分:0)
感谢frank,我能够通过进入firebase并允许读取数据来解决此问题。