In the app I'm fetching a value from core data (this value is a date) and comparing it to a time(in the format of a string) and doing actions accordingly. However for some reason the if statement just doesn't seem to work. No matter which time it is the result is always using value1 (time 0:00 to 5:30) for the division (please check code below). I have checked to see if the core data fetching has the correct name and yes it should be working. Anyone have any ideas?
func calculate() {
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)
}
}
thanks