当有明确的数据时,Firebase字典返回nil

时间:2016-11-20 20:50:40

标签: swift firebase-realtime-database

这是我的数据库结构:

orders_Placed {
  orderID {"date":...etc}
} 

我正在尝试检索所有订单,但dictionary.value为零,尽管打印时的字典明确包含数据。

代码:

ref.child("orders_Placed").observe(.childAdded, with: { snapshot in

    if let tes = snapshot.value as? Dictionary<String, AnyObject> {
        print("snapshot dictionarty is snapshot \(tes.reversed())")
        for t in tes {
            print("\nsnapshot dictionarty name snapshot \(t.value["date"])") //*1

            print("\nsnapshot dictionarty name key T IS \(t)") //*2
        }}

输出

snapshot dictionarty name snapshot nil //*1

snapshot dictionarty name key T IS ("date", 1479580695307) //*2 clearly has data but for some reason returns nil?

1 个答案:

答案 0 :(得分:1)

您使用的for循环,t实际上是(key: String, value: AnyObject)类型的元组。要访问实际的keyvalue,您只需分别执行t.keyt.value。由于key不是字典,因此无需再将字符串t传递给它。

for t in tes
{
  var currentDateKey = t.key // the key associated to the date
  var currentDate = t.value // extracts the exact value
}