gpt4 book ai didi

swift - 计算耗时并更新所有任务

转载 作者:行者123 更新时间:2023-11-30 10:08:12 24 4
gpt4 key购买 nike

我正在创建一个待办事项列表应用程序,它以秒为单位倒计时,直到下一个任务。

一旦完成一项任务,计时器就会开始倒计时,直到下一项任务,依此类推。

当应用程序打开时,这种情况会实时发生,但是当应用程序关闭时,计时器将停止,并且当前日期和时间存储在 NSUserDefaults 中,以便可以在打开时加载它们。

当应用程序打开时,我希望能够执行如下简单的操作:

let nextTaskIn   = 600.0 // example of were the last task was
let appClosed = "2016-01-19 23:00:49 +0000" // example of time closed really comes from NSDate set on close
let timePassed = NSDate().timeIntervalSinceDate(appClosed) // get num of seconds changed
let nextTaskTime = nextTaskIn - timePassed // example 600.0 - 120.0 = 480 seconds left

然后我可以将文本标签更新为 nextTaskTime 一切都很棒!

但是,当耗时比当前任务长并占用下一个任务时,就会出现问题。

例如:

let task1 = 90.0
let task2 = 180.0
let task3 = 270.0
let timePassed = 110.0
let nexTaskTime = task1 - timePassed // this is not correct

这是行不通的,它应该做的是使 task1 = 0.task2 = 160.0 但递归地遍历所有或尽可能多的任务,直到已经没有了。

为了让它工作,我最好的想法是一个 while 循环,但我能想出的只是一个跟随,我已经没有想法了。

let task1 = 90.0
let task2 = 180.0
let task3 = 270.0
var timePassed = 110.0

while timePassed > 0 {
let takenAway = timePassed - task1 // but unsure how to swap out for next task if task = 0
timePassed = takenAway
}

最佳答案

不确定这是否符合您的问题

let task1 = 90.0
let task2 = 180.0
let task3 = 270.0
var timePassed = 300.0
var tasks: [Double] = [task1, task2, task3] // init an array of your task

func updateTask(inout timePassed: Double) {
for (index, _) in tasks.enumerate() { // enumerate your task
let takenAway = timePassed - tasks[index]
if takenAway <= 0 { // func should end here
timePassed = 0
tasks[index] = abs(takenAway)
return
}else { // keep going
timePassed = takenAway
tasks[index] = 0
}
}
}

updateTask(&timePassed)

关于swift - 计算耗时并更新所有任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34888902/

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