gpt4 book ai didi

ios - 将 TimeInterval 转换为字符串 String To Time Interval

转载 作者:可可西里 更新时间:2023-11-01 00:02:20 26 4
gpt4 key购买 nike

我有一个 TimeInterval 我必须将它变成一个 String 以便它可以添加到从 Apple Watch 传递的 Dictionary通过 WatchConnectivity 连接到 iPhone。

现在我在 iPhone 上有了 String,我需要把它变回 TimeInterval,但我似乎不能弄清楚这一点。

(我需要在显示锻炼持续时间的 UILabel 中显示它。)

有什么想法吗?

例如

观看:

00:15:15(时、分、秒)变成“915.012948989868”

let myDouble = computeDurationOfWorkout(withEvents: hkWorkout?.workoutEvents, startDate: hkWorkout?.startDate, endDate: hkWorkout?.endDate)
let myDoubleString = String(myDouble)
durationVariableString = myDoubleString

iPhone:

需要将“915.012948989868”转回00:15:15

最佳答案

使用 calendar.dateComponents 可以将 915 转换为 00:15:15

func stringFromTimeInterval (interval: String) -> String {
let endingDate = Date()
if let timeInterval = TimeInterval(interval) {
let startingDate = endingDate.addingTimeInterval(-timeInterval)
let calendar = Calendar.current

var componentsNow = calendar.dateComponents([.hour, .minute, .second], from: startingDate, to: endingDate)
if let hour = componentsNow.hour, let minute = componentsNow.minute, let seconds = componentsNow.second {
return "\(hour):\(minute):\(seconds)"
} else {
return "00:00:00"
}

} else {
return "00:00:00"
}
}

关于ios - 将 TimeInterval 转换为字符串 <del>String To Time Interval</del>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40893338/

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