【发布时间】:2015-07-07 15:28:48
【问题描述】:
我在 xcode 中为应用程序制作了一个计时器,我正在尝试平均时间,但为了做到这一点,我需要将 NSTimer 的输出字符串保存到一个变量中。我将它格式化为三个字符串,读为一个,看起来像“00:00:00”,带有分钟、秒和毫秒。如何将单个块保存为变量?我知道如何使用子字符串来隔离片段,我只需要将它们保存为变量。
func updateTime() {
var currentTime = NSDate.timeIntervalSinceReferenceDate()
//Find the difference between current time and start time.
var elapsedTime: NSTimeInterval = currentTime - startTime
//calculate the minutes in elapsed time.
let minutes = UInt8(elapsedTime / 60.0)
elapsedTime -= (NSTimeInterval(minutes) * 60)
//calculate the seconds in elapsed time.
let seconds = UInt8(elapsedTime)
elapsedTime -= NSTimeInterval(seconds)
//find out the fraction of milliseconds to be displayed.
let fraction = UInt8(elapsedTime * 100)
//add the leading zero for minutes, seconds and millseconds and store them as string constants
let strMinutes = minutes > 9 ? String(minutes):"0" + String(minutes)
let strSeconds = seconds > 9 ? String(seconds):"0" + String(seconds)
let strFraction = fraction > 9 ? String(fraction):"0" + String(fraction)
//concatenate minuets, seconds and milliseconds as assign it to the UILabel
timerLabel.text = "\(strMinutes):\(strSeconds):\(strFraction)"
}
那里是我更新时间的地方,不知道你还需要看什么。
【问题讨论】:
标签: xcode string swift variables var