快速检索数据Firebase

时间:2018-09-28 00:03:24

标签: swift firebase firebase-realtime-database

我正在尝试检索已将其保存在对象中的字符串键

enter image description here

  func retrieveData() {
    let refAll = Database.database().reference().child("Playground")
    refAll.observe(.value) { (snapshot) in
        if let snapshotValue = snapshot.value as? [String:Any] {
            var playgroundSnapshot = snapshotValue
            let playgroundKeys = Array(playgroundSnapshot.keys)
            self.playgroundArray.removeAll()
            for key in playgroundKeys  {
                guard
                    let value = playgroundSnapshot[key] as? [String : Any]
                    else {
                        continue
                }
                let title = value["title"] as! String
                let city = value["city"] as! String
                let location = value["location"] as! String
                let price = value["price"] as! String

                let playground = Playground(title: title, price: price, location: location, city: city, availblePlayground: true)
                self.playgroundArray.append(playground)
            }
            self.tabeView.reloadData()
        }
    }
}

每个人在每个对象上都有一个键

 keySelected = playgroundArray[indexPath.row].key

但是我不知道为什么keySelected甚至为游乐场数组都具有对象时也为零

1 个答案:

答案 0 :(得分:0)

playgroundArray不包含“ key”。您必须设置游乐场结构。添加var key:String?,并将其添加到init()函数中。

let playground = Playground(title: title, price: price, location: location, city: city, availblePlayground: true)

let playground = Playground(title: title, price: price, location: location, city: city, availblePlayground: true, key: key)


self.playgroundArray.append(playground)