在我的第一个应用程序中,我有一个函数作为 [String:String]
字典发布到 Firebase,在 Firebase 中我看到发布的所有值都是 String
。在我的第二个应用程序中,我有一个读取该节点并检索为 [String:String]
的函数,但在快照中打印两个值,Booking Date
和 预订 ID
看起来像 Int
。我实际上让它们 Int
就像我需要的那样,当我在模拟器 iOS 12.1
上运行我的第二个应用程序时,它并不是很好并且按预期工作,但是当我在我的 iPad 3 iOS 9.3.5
上运行应用程序崩溃,因为 let bookingId = Int(value["Booking Id"]!)
为 nil,但这并没有发生使用let bookingDate = Int(value["Booking Date"]!)
。 iPad 好像没有
比如转换多个值。
你有什么解决办法吗?
这是函数:
func getMyBookings() {
let ref = Database.database().reference()
ref.child("Continent").child("Europe").child("Country").child("Italy").child("Region").child("Emilia-Romagna").child("City").child("Bologna").child("Shops").child("Workshop Bookings").queryOrdered(byChild: "Shop Name").queryEqual(toValue: "Spezial Cycle").observe(.value) { (snapshot) in
// ref.child("Continent").child("Europe").child("Country").child("Italy").child("Region").child("Emilia-Romagna").child("City").child("Bologna").child("Shops").child("Workshop Bookings").queryOrdered(byChild: "Shop Name").queryEqual(toValue: "Spezial Cycle").observe(.childAdded, with: { (snapshot) in
print(snapshot)
if let data = snapshot.value as? [String: [String:String]] {
for (_, value) in
data{
let bookingDate = Int(value["Booking Date"]!)!
// let bookingDate = value["Booking Date"]!
print("bookingDate is: \(String(describing: bookingDate))")
let bookingStart = value["Booking Start"]
let bookingEnd = value["Booking End"]
let customerName = value["User Name"]
let bookingId = Int(value["Booking Id"]!)!
print("bookingId is :\(String(describing: bookingId))")
let booking: (bookingDate: Int, bookingStart: String, bookingEnd: String, customerName: String, bookingId: Int) = (bookingDate!, bookingStart!, bookingEnd!, customerName!, bookingId: bookingId)
print("booking is: \(booking)")
self.bookingsArray.append(booking)
self.calculateBookedTimeSlots()
}
self.calculateBookedTimeSlots()
if #available(iOS 10.0, *) {
let actions: [UNNotificationAction] = [UNNotificationAction(identifier: "chiudi", title: "Chiudi", options: [.foreground])]
Notifications.newTimeIntervalNotification(notificationType: "New booking", actions: actions, categoyIdentifier: "New Booking", title: "Nuova prenotazione", body: "Hai una nuova prenotazione", userInfo: [:], timeInterval: 5, repeats: false)
} else if #available(iOS 9.0, *){
// Fallback on earlier versions
Notifications.newTimeIntervalNotification(notificationType: "New booking", actions: [], categoyIdentifier: "New Booking", title: "Nuova prenotazione", body: "Hai una nuova prenotazione", userInfo: [:], timeInterval: 5, repeats: false)
}
}
// })
}
}
这是显示 Booking Date
和 Booking Id
的快照打印,看起来像 Int
:
Snap (Workshop Bookings) {
"-Lb4XzGtLtnBAgoPB6Ay" = {
"Booking Date" = 20190329;
"Booking End" = "14:00";
"Booking Id" = 201903291300;
"Booking Start" = "13:00";
"Shop Logo Url" = "https://firebasestorage.googleapis.com/v0/b/fix-it-b4b00.appspot.com/o/Spezial%20Cycle%2FSpezial%20Cycle%20logo.png?alt=media&token=016cc976-ae8d-4c71-a77f-a899d661be20";
"Shop Name" = "Spezial Cycle";
"User Name" = "";
"Works List" = "Revisione Generale, ";
};
}
最佳答案
我认为问题在于您的 iPad 3 仍在 32 位数据总线上运行,并且值 201903291300 稍微太大而无法放入 Int32
(即 Int
在 32 位架构上的实现)。
你应该尝试使用 Int64
而不是 Int
关于ios - 在 iPad3 上而不是在模拟器上运行应用程序时,我在变量上得到 nil。 swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55406427/