gpt4 book ai didi

swift - 检查时间是否在范围内

转载 作者:搜寻专家 更新时间:2023-11-01 07:27:29 24 4
gpt4 key购买 nike

在应用程序中,我从核心数据中获取一个值(这个值是一个日期)并将它与一个时间(以字符串的格式)进行比较,并相应地执行操作。但是由于某种原因,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)
}

}

谢谢

最佳答案

我在 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 并将分钟添加到它的末尾并进行比较。

关于swift - 检查时间是否在范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35024245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com