在Swift中比较两个计时器值
我有一个简单的Swift游戏,它使用一个定时器对象来跟踪用户存活的时间。我希望能够跟踪他们的最佳时间。我目前正在如下配置计时器:在Swift中比较两个计时器值,swift,algorithm,timer,comparison,Swift,Algorithm,Timer,Comparison,我有一个简单的Swift游戏,它使用一个定时器对象来跟踪用户存活的时间。我希望能够跟踪他们的最佳时间。我目前正在如下配置计时器: func startGameTimer() { timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(updateTimer)), userInfo: nil, repeats: true) } func updateTimer() { sec
func startGameTimer() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(updateTimer)), userInfo: nil, repeats: true)
}
func updateTimer() {
seconds += 1
activeTimer.text = timeString(time: TimeInterval(seconds))
}
func timeString(time:TimeInterval) -> String {
let minutes = Int(time) / 3600
let seconds = Int(time) / 60 % 60
let milliseconds = Int(time) % 60
return String(format:"%02i:%02i.%02i", minutes, seconds, milliseconds)
}
当玩家输了,他们的时间以字符串的形式显示:“mm:ss.ms”
我想将UserDefaults中存储的值与当前计时器值进行比较,以确定哪个时间是最佳时间
我已经看到了一些关于日期对象的问题,但这显然不是一天中的某个时间,也不使用小时
在不分析和比较时间的每个部分的情况下,比较这些值的最佳方法是什么?启动计时器时,可以使用var start=Date.timeintervalscenereferencedate之类的值,然后var stop=Date.timeintervalscenereferencedate,然后测量时间并将其与用户默认值中的时间进行比较。这是我解析和比较计时器值所做的。同样,值以“mm:ss.ms”格式存储为字符串 此函数将UserDefaults中的最佳时间变量设置为最高计时器值
func determineBestTime() {
if UserDefaults.standard.value(forKey: "bestTime") == nil {
UserDefaults.standard.set(currentTime, forKey: "bestTime")
bestTimeLabel.text = activeTimerLabel.text
bestTime = UserDefaults.standard.value(forKey: "bestTime") as! String
} else {
let bestTimeValues = bestTime.components(separatedBy: ":")
let currTimeValues = currentTime.components(separatedBy: ":")
let bestTimeMin = Int(bestTimeValues[0])!
let currTimeMin = Int(currTimeValues[0])!
if currTimeMin < bestTimeMin {
return
} else if currTimeMin > bestTimeMin {
UserDefaults.standard.set(currentTime, forKey: "bestTime")
bestTime = UserDefaults.standard.value(forKey: "bestTime") as! String
bestTimeLabel.text = bestTime
return
}
let bestTimeVals = bestTimeValues[1].components(separatedBy: ".")
let currTimeVals = currTimeValues[1].components(separatedBy: ".")
let bestTimeSec = Int(bestTimeVals[0])!
let currTimeSec = Int(currTimeVals[0])!
if currTimeSec < bestTimeSec {
return
} else if currTimeSec > bestTimeSec {
UserDefaults.standard.set(currentTime, forKey: "bestTime")
bestTime = UserDefaults.standard.value(forKey: "bestTime") as! String
bestTimeLabel.text = bestTime
return
}
let bestTimeMilliSec = Int(bestTimeVals[1])!
let currTimeMilliSec = Int(currTimeVals[1])!
if currTimeMilliSec < bestTimeMilliSec {
return
} else if currTimeMilliSec > bestTimeMilliSec {
UserDefaults.standard.set(currentTime, forKey: "bestTime")
bestTime = UserDefaults.standard.value(forKey: "bestTime") as! String
bestTimeLabel.text = bestTime
}
}
}
func determineebesttime(){
如果UserDefaults.standard.value(forKey:“bestTime”)==nil{
UserDefaults.standard.set(currentTime,forKey:“bestTime”)
bestTimeLabel.text=activeTimerLabel.text
bestTime=UserDefaults.standard.value(forKey:“bestTime”)作为!字符串
}否则{
设bestTimeValues=bestTime.components(以“:”分隔)
让currTimeValues=currentTime.components(以“:”分隔)
设bestTimeMin=Int(bestTimeValues[0])!
让currtimen=Int(currTimeValues[0])!
如果currTimeMinbestTimeMin,则为else{
UserDefaults.standard.set(currentTime,forKey:“bestTime”)
bestTime=UserDefaults.standard.value(forKey:“bestTime”)作为!字符串
bestTimeLabel.text=最佳时间
返回
}
设bestTimeVals=bestTimeValues[1]。组件(以“.”分隔)
设currTimeVals=currTimeValues[1]。组件(以“.”分隔)
设bestTimeSec=Int(bestTimeVals[0])!
设currTimeSec=Int(currTimeVals[0])!
如果CurrtTimeSecbestTimeSec,则为else{
UserDefaults.standard.set(currentTime,forKey:“bestTime”)
bestTime=UserDefaults.standard.value(forKey:“bestTime”)作为!字符串
bestTimeLabel.text=最佳时间
返回
}
设BestTimeMillissec=Int(bestTimeVals[1])!
设currtimemillissec=Int(currTimeVals[1])!
如果currtimemillissecBestTimeMillissec,则为else{
UserDefaults.standard.set(currentTime,forKey:“bestTime”)
bestTime=UserDefaults.standard.value(forKey:“bestTime”)作为!字符串
bestTimeLabel.text=最佳时间
}
}
}
存储并比较数字。仅转换为字符串用于显示。虽然我相信这会起作用,但我已经设置好计时器并开始工作。按照上面的建议,制作解析器并比较int可能更好。这是解决问题最糟糕的方法。不要在UserDefaults中存储字符串。存储一个Int
。它更容易储存,更容易比较。不要将Int
转换为String
,除非您需要以所需的格式显示它。@rmaddy我知道这可能不是最好的解决方案,但这就是问题的重点。lol然而,我可能只是误解了你,但我不认为存储Int是可能的。如果你看第一个问题,我的timer方法形成了一个“mm:ss.s”字符串,我不知道你所说的存储Int是什么意思?你的定时器正在增加你的秒数
变量。这是一个Int
。存储该值。不要存储您根据数字创建的字符串。@rmaddy lol-wow,我完全忽略了这一点。。。嗯。用这句话作为回答,我会接受的。谢谢你的关注。