【发布时间】: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