Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 将秒转换为时间戳_Swift_Xcode_Time - Fatal编程技术网

Swift 将秒转换为时间戳

Swift 将秒转换为时间戳,swift,xcode,time,Swift,Xcode,Time,我正在使用Swift计算一个时间数组,以百分之一秒为单位计算平均次数。得到的平均值是总百分之一秒的输出 例:总百分之一秒111 我怎样才能把这一百秒转换成 分:秒。第一百秒 最大60:最大60。最多100 ex:00:01.11 我使用此函数将数组转换为总百分之一秒: let timeToSecondsArray = valueLast20.map{ i -> Int in let timeString = (i as AnyObject).components(sep

我正在使用Swift计算一个时间数组,以百分之一秒为单位计算平均次数。得到的平均值是总百分之一秒的输出

例:总百分之一秒111

我怎样才能把这一百秒转换成

分:秒。第一百秒

最大60:最大60。最多100

ex:00:01.11

我使用此函数将数组转换为总百分之一秒:

let timeToSecondsArray = valueLast20.map{
     i -> Int in
     let timeString = (i as AnyObject).components(separatedBy: ":")
     let minute = Int(timeString[0])
     let second = Int(timeString[1])
     let hundredthseconds = Int(timeString[2])
     let minuteToSec = minute! * 3600
     let secondToSec = second! * 60
     let totalSeconds = minuteToSec + secondToSec + hundredthseconds!
     return totalSeconds
}

let seconds: Int = timeToSecondsArray.reduce(0, {x, y in x + y})/timeToSecondsArray.count
let one = String(format: "%02ld", seconds/3600)
let two = String(format: "%02ld", (seconds%3600)/60)
let three = String(format: "%02ld", ((seconds%3600)%60))
self.averageTime.text = "\(one):\(two).\(three)"
01:02.11


你的问题是什么?不管怎么说->毫秒111秒不是1分11秒。好吧,让我们重新开始:它实际上不是毫秒,而是百分之一秒。你做错了的是假设111秒的输入是秒,不是,那是111百分之一秒,意味着一整秒和11多百分之一秒。@WillMays这不是苹果秒表计时的方式。Apple秒表格式为00:00.00,注意小数点,表示分、秒和百分之一秒,而不是毫秒。克里斯告诉你的是正确的,111秒是1分51秒。@luk2302啊,好的,我现在明白了,我已经更新了问题
let valueLast20 = ["01:03:24", "01:00:98"]

let hundredthSecondsArray = valueLast20.map{
    i -> Int in
    let timeString = (i as AnyObject).components(separatedBy: ":")
    let minute = Int(timeString[0])
    let second = Int(timeString[1])
    let hundredthSeconds = Int(timeString[2])!
    let minuteToHundredth = minute! * 60 * 100
    let secondToHundredth = second! * 100
    let totalSeconds = minuteToHundredth + secondToHundredth + hundredthSeconds
    return totalSeconds
}

let hundredth: Int = hundredthSecondsArray.reduce(0, +) / hundredthSecondsArray.count
let one = String(format: "%02ld", hundredth / 100 / 60)
let two = String(format: "%02ld", (hundredth / 100 % 60))
let three = String(format: "%02ld", (hundredth % 100))
print("\(one):\(two).\(three)")