更具体的说,我的功能(incomeToday)正在累加数字,然后当日期发生变化时,我希望将当天的收入加到总数中除以数字(自开始以来的天数 - 那部分工作)所以我然后看到平均收入pr。天:
[ (0, [{decimal = 1M;}]);
(1, [{decimal = 2M;}; {decimal = 18M;}]);
(2, [{decimal = 3M;}]);
(3, [{decimal = 4M;}; {decimal = 5M;}; {decimal = 24M;};
{decimal = 6M;}; {decimal = 7M;}]);
(4, [{decimal = 8M;}; {decimal = 9M;}; {decimal = 10M;}]) ]
所以这是我希望在Date更改时(00:00)运行的函数TotalIncome()。这是可能的,如果可能的话怎么样? (我正在使用NSDate()进行图像处理,但根本不确定如何)。
非常感谢!!
答案 0 :(得分:0)
此功能每天调用一次。只有在日期改变时才会调用它。在appdelegate中调用此函数。
let defaults = NSUserDefaults.standardUserDefaults()
let date = NSDate()
let frmt = NSDateFormatter()
frmt.dateFormat = "YYYY-MM-dd"
let today = frmt.stringFromDate(date)
if let savedDate = defaults.objectForKey("today")
{
if self.compareTwoDates(savedDate as! String, secondDate: today) == false {
// perform calculations
// this block gets called if the date is changed
}
} else {
// perform calculations
// this block gets called for the first time only.
}
self.defaults.setValue(today, forKey: "today")
// here we save the today's date for next day's comparison
日期比较功能是
func compareTwoDates(firstDate: String, secondDate: String) -> Bool {
let frmt = NSDateFormatter()
frmt.dateFormat = "YYYY-MM-dd"
let date_first = frmt.dateFromString(firstDate)
let date_second = frmt.dateFromString(secondDate)
return (date_first?.isEqualToDate(date_second!))!
}