简体   繁体   中英

The compiler is unable to type-check this expression

I want to divide difference data into 60 and print it as double numbers. When I print it as a string, it does not appear to be a fraction of the number. I get this problem when I print the number "n" . What should I do?

My mistake: the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

if let date = formatter.date(from: receivedTimeString) {
    let receivedTimeHoursMinutes = Calendar.current.component(.hour, from: date) * 60
    let receivedTimeMinutes = Calendar.current.component(.minute, from: date)
    let totalreceivedTimeMinutes = receivedTimeHoursMinutes + receivedTimeMinutes

    let todayHoursMinutes = Calendar.current.component(.hour, from: Date()) * 60
    let todayMinutes = Calendar.current.component(.minute, from: Date())
    let todayTimeMinutes = todayHoursMinutes + todayMinutes

    let difference = todayTimeMinutes - totalreceivedTimeMinutes
    let str = String(difference)

    switch true {
    case difference > 60:
        let deger = String(difference / 60)
        guard let n = NumberFormatter().number(from: deger) else { return }

        print("deger", deger)
        self.labelTimerFarkSonuc.text = (n) + (" ") + ("Saattir") + (" ") + (durum)
    case difference == 0:
        self.labelTimerFarkSonuc.text = (n) + (" ") + ("Dakikadır")  + (" ") + (durum)
    case difference < 60:
        self.labelTimerFarkSonuc.text = (n) + (" ") + ("Dakikadır")  + (" ") + (durum)
    default:
        self.labelTimerFarkSonuc.text = (n) + (" ") + ("Dakikadır")  + (" ") + (durum)
    }

If i had understood your question correctly,

If you want to have result of following code as decimal fraction,

let deger = String(difference / 60) // Dividing by INT will not give fractions.

Change it to following.

let deger = String(difference / 60.0)

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