我正在尝试从我的 Firebase 实时数据库中提取值,但无论出于何种原因,这些值都以 NSNull 的形式返回。在我将 'let count = ...' 更改为 'if let' 之前,我在第 13 行收到错误消息“无法将 'NSNull' 类型的值转换为 'NSNumber'。我不确定为什么这些值返回 null 而不是。
我已经尝试打印检索到的快照,并且正如我所料,没有一个值是空的。我难住了。这是打印检索到的快照时得到的日志:
[Snap (A12-1A) 0, Snap (B1-2A) 0, Snap (C2-3A) 0, Snap (D3-4A) 0, Snap (E4-5A) 0, Snap (F5-6A) 0, Snap (G6-7A) 0, Snap (H7-8A) 0, Snap (I8-9A) 0, Snap (J9-10A) 5, Snap (K10-11A) 0, Snap (L11-12P) 0, Snap (M12-1P) 0, Snap (N1-2P) 0, Snap (O2-3P) 0, Snap (P3-4P) 0, Snap (Q4-5P) 0, Snap (R5-6P) 0, Snap (S6-7P) 0, Snap (T7-8P) 0, Snap (U8-9P) 0, Snap (V9-10P) 0, Snap (W10-11P) 0, Snap (X11-12A) 0]
此快照中的所有数据都是我的 Firebase 数据库中的数据,所以我不确定为什么会出现此错误。
我的 Firebase 设置:日历注册 --- 04132019 --- (A12-1A: 0, B1-2A: 0, ..., X11-12P: 0)
func getCalendarSignupsOnDate(selectedDate: String, handler: @escaping (_ dateSignups: [Calendar]) -> ()) {
var calendarArray = [Calendar]()
REF_CALENDAR_SIGNUPS.observeSingleEvent(of: .value) { (snapshot) in
guard let snapshot = snapshot.children.allObjects as? [DataSnapshot] else { return }
var doesContainDate = false
for date in snapshot {
if date.key == selectedDate {
doesContainDate = true
self.REF_CALENDAR_SIGNUPS.child(date.key).observeSingleEvent(of: .value, with: { (dateSnapshot) in
guard let dateSnapshot = dateSnapshot.children.allObjects as? [DataSnapshot] else { return }
for timeSlots in dateSnapshot {
let time = timeSlots.key
if let count = timeSlots.childSnapshot(forPath: "J9-10A").value as? Int {
let calendarData = Calendar(time: time, signupCount: count)
calendarArray.append(calendarData)
handler(calendarArray)
} else {
let count = 0
let calendarData = Calendar(time: time, signupCount: count)
calendarArray.append(calendarData)
handler(calendarArray)
}
}
})
}
}
for item in calendarArray {
print(item.time + " " + String(item.signupCount))
}
if doesContainDate == false {
let dateSignUpData = [
"A12-1A": 0,
"B1-2A": 0,
"C2-3A": 0,
"D3-4A": 0,
"E4-5A": 0,
"F5-6A": 0,
"G6-7A": 0,
"H7-8A": 0,
"I8-9A": 0,
"J9-10A": 0,
"K10-11A": 0,
"L11-12P": 0,
"M12-1P": 0,
"N1-2P": 0,
"O2-3P": 0,
"P3-4P": 0,
"Q4-5P": 0,
"R5-6P": 0,
"S6-7P": 0,
"T7-8P": 0,
"U8-9P": 0,
"V9-10P": 0,
"W10-11P": 0,
"X11-12A": 0
]
as [String : Int]
self.REF_CALENDAR_SIGNUPS.child(selectedDate).updateChildValues(dateSignUpData)
for timeSlots in dateSignUpData {
let time = timeSlots.key
let count = timeSlots.value
let calendarData = Calendar(time: time, signupCount: count)
calendarArray.append(calendarData)
}
}
handler(calendarArray)
}
}