There is an idea but I do not know if it is possible to implement it. When adding a result, two Float values (hours / minutes) and NSDate (the date the result was added) are stored in CoreData. How to add up the added hours in a month? Even if the hours / minutes and the date are stored as NSDate, are there any methods to break everything up by months / years? There are some ways to display statistics.
PS Alternatively, the clock can be added to a separate array and displayed as the sum of all values of the array, and you can also assign the name of the month to another variable. Set a condition when adding a result, for example, if the current date (month) of adding differs from a variable (with the name of the month), then delete everything from the array and add a new value.
UPD
In the body of the function of adding the result, wrote the code. I described the above idea, here is a part of the code:
@IBAction func wTime() { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let managedObjectContext = appDelegate.managedObjectContext tMoney = NSEntityDescription.insertNewObjectForEntityForName("WorkTime", inManagedObjectContext: managedObjectContext) as! WorkTime ///Работа с пикерами let deltaYesterdayDate = yesterdayDate.date.dateByAddingTimeInterval(addTime) //второй пикер минус время ///нахождение разницы во времени let components = NSCalendar.currentCalendar().components([.Month, .Hour, .Minute], fromDate: todayDate.date, toDate: deltaYesterdayDate, options: .WrapComponents) ///результаты let diffHour = Int(components.hour) //Часы let diffMinute = Int(components.minute) ///Минуты //текущий месяц из результата let monthResult = (components.month) //Массив для часов var arrayHours = [Int]() let settings = NSUserDefaults.standardUserDefaults().floatForKey("settings") //переменная для условия сравнения месяца var monthWas = Int(tMoney.currentMonth!) //переменная для первого запуска т.к. при первом запуске currentMonth не имеет значения в CoreData var firstLaunch = 0 //первое условие по идее должно выполняться только один раз if firstLaunch == 0 && monthWas != monthResult { let hourForArray = diffHour * 60 arrayHours.append(hourForArray) arrayHours.append(diffMinute) } ///это условие выполняется если сохраненный месяц равен месяцу,который берется из DatePicker else if monthWas == monthResult { let hourForArray = diffHour * 60 arrayHours.append(hourForArray) arrayHours.append(diffMinute) } //если результат добавляется в новом месяце else if monthResult > monthWas { tMoney.currentMonth = Int(monthResult) //добавить новое значение в CoreData arrayHours.removeAll() //очистить массив let hourForArray = diffHour * 60 arrayHours.append(hourForArray) arrayHours.append(diffMinute) } //если месяц добавления равен 1(например январь 2017) else if monthResult < monthWas && monthWas == 12 { tMoney.currentMonth = Int(monthResult) arrayHours.removeAll() let hourForArray = diffHour * 60 arrayHours.append(hourForArray) arrayHours.append(diffMinute) } //сумма элементов массива var sumHours = Float(arrayHours.reduce(0, combine: +)) //общее кол-во денег для отображения за месяц var sumMoneyForMonth = Float(sumHours * (settings/60)) var summoneyAll = Float(sumMoneyForMonth - (sumMoneyForMonth * 0.13)) tMoney.allTime = Float(sumHours) tMoney.allMoney = Float(summoneyAll) tMoney.currentMonth = Int(monthResult) appDelegate.saveContext() }
The problem is that the elements added to the array are not saved. How to make so that they are saved and deleted if necessary?