【问题标题】:Checking to see if time is within range检查时间是否在范围内
【发布时间】:2016-05-03 15:07:37
【问题描述】:

在应用程序中,我从核心数据中获取一个值(该值是一个日期)并将其与一个时间(以字符串的格式)进行比较并相应地执行操作。但是由于某种原因, if 语句似乎不起作用。无论是哪个时间,结果总是使用 value1(时间 0:00 到 5:30)进行除法(请检查下面的代码)。我检查了核心数据获取是否具有正确的名称,是的,它应该可以工作。谁有想法?

函数计算() {

        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext
        let fetchRequest = NSFetchRequest(entityName: "Settings")

        do {
            let results = try managedContext.executeFetchRequest(fetchRequest)
            Settings = results as! [NSManagedObject]

            for result in results as! [NSManagedObject] {

                correctionRatio = result.valueForKey("correctionRatio") as! Int
                target = result.valueForKey("targetbG") as! Int
                value1 = result.valueForKey("value1") as! Int
                value2 = result.valueForKey("value2") as! Int
                value3 = result.valueForKey("value3") as! Int
                value4 = result.valueForKey("value4") as! Int
                value5 = result.valueForKey("value5") as! Int

                if bGTextfield.text != "" && carbTextfield.text != "" {

                    current = Int(bGTextfield.text!)
                    carb = Int(carbTextfield.text!)

                    let currentTime = NSDate()
                    let timeFormatter = NSDateFormatter()
                    timeFormatter.locale = NSLocale.currentLocale()
                    timeFormatter.dateFormat = "HH:mm"
                    let time = timeFormatter.stringFromDate(currentTime)


                    if time > "00:00" && time < "5:30" {
                        food = carb / value1
                    } else if time > "5:31" && time < "11:00"{
                        food = carb / value2
                    } else if time > "11:01" && time < "17:00"{
                        food = carb / value3
                    } else if time > "17:01" && time < "21:30" {
                        food = carb / value4
                    } else if time > "21:31" && time < "23:59" {
                        food = carb / value5
                    }



                    if 4 ... 9 ~= Double(currentbG){

                        doseLabel.text = String(foodInsulin)


                    } else if 9.1 ... 100 ~= Double(currentbG) {

                        bgDiff = currentbG - targetbG
                        correction = bgDiff / correctionRatio
                        total = food + correctionInsulin

                        doseLabel.text = String(total)
                    }

    }

谢谢

【问题讨论】:

  • 感谢大家的信息。我会努力并尽快回复大家

标签: swift core-data nsdate nsdateformatter nsdatecomponents


【解决方案1】:

我在 NSDate 扩展中做了类似的事情:

private class func timeAsIntegerFromDate(date: NSDate) -> Int {
    let currentCal = NSCalendar.currentCalendar()
    currentCal.timeZone = NSTimeZone.localTimeZone()
    let comps: NSDateComponents = currentCal.components([NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: date)
    return comps.hour * 100 + comps.minute
}

private class func timeIsBetween(startDate: NSDate, endDate: NSDate) -> Bool {
    let startTime = NSDate.timeAsIntegerFromDate(startDate)
    let endTime = NSDate.timeAsIntegerFromDate(endDate)
    let nowTime = NSDate.timeAsIntegerFromDate(NSDate())

    if startTime == endTime { return false }

    if startTime < endTime {
        if nowTime >= startTime {
            if nowTime < endTime { return true }
        }
        return false
    } else {
        if nowTime >= startTime || nowTime < endTime {
            return true
        }
        return false
    }
}

我相信它可以稍微清理一下并进行很大改进。我所做的是将NSDate 变成Int,然后检查哪个更大更小。基本原理是这将时间乘以 100,然后将分钟添加到它的末尾并进行比较。

【讨论】:

    【解决方案2】:

    只是一种预感,但我认为您无法将时间与 > 和

    Time comparisons in swift

    那篇文章解释了如何正确地做到这一点。

    此外,您将时间存储为字符串而不是日期。将其存储为日期。

    【讨论】:

    • 嗨,不正是我要找的。比较对我没有帮助,因为我试图确定当前时间是否在一个范围内
    • 您在 if 语句中进行了比较测试。您正在将两个字符串与 > 和
    【解决方案3】:

    您正在对本质上是数字的值进行字符串比较。从几个不同的方面来看,这是一个坏主意,这会导致您的结果不准确。您可以像这样比较字符串,但规则与数字比较不同。

    如果要比较时间,请比较时间。您可以从以下内容开始:

    let components = NSCalendar.currentCalendar().componentsInTimeZone(NSTimeZone.localTimeZone(), fromDate: NSDate())
    

    从那里,components 将具有名为 hourminute 的数字属性。使用这些进行比较。

    【讨论】:

      【解决方案4】:

      如果是日期,您希望将日期存储为 NSDate。或者作为Int16,如果它是一天中的分钟minutes + 60 * hours 这样05:30 将是5 * 60 + 30 = 330。一旦你完成了,创建一个与这些值匹配的谓词就很简单了你感兴趣的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多