这是我的数据库结构:
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?
答案 0 :(得分:1)
您使用的for循环,t
实际上是(key: String, value: AnyObject)
类型的元组。要访问实际的key
或value
,您只需分别执行t.key
或t.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
}