简体   繁体   中英

I get nil on a variable when running app on iPad3 but not on simulator. Swift 4

In my 1st app I have a function that posts to Firebase as [String:String] dictionary, and in Firebase I see all values posted are String . In my 2nd app I have a function that reads that node and retrieves as [String:String] also, but in the snapshot print two of the values, Booking Date and Booking Id look like Int . I than actually make them Int like I need them to be and when I run my 2nd app on simulator iOS 12.1 it't all good and works as expected, but when I run it on my iPad 3 iOS 9.3.5 app crashes because let bookingId = Int(value["Booking Id"]!) is nil, but that doesn't happen with let bookingDate = Int(value["Booking Date"]!) . Looks like iPad doesn't like converting more than one value. Do you see any solution to this? Here is the function:

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)
            }
        }
        //        })
    }
}

And this is the snapshot print showing both Booking Date and Booking Id looking like 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, ";
    };
} 

I think the problem is that your iPad 3 is still running on a 32 bit data bus, and the value 201903291300 is slightly too large to fit into that Int32 (which is the Int implementation on 32 bit architecture).

You should try to use Int64 instead of Int

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM