如何更改标签的时间格式? Swift

How can I change time format of a label ? Swift

在我的应用程序中,我想向用户显示他需要的时间。目前它看起来像这样: 这是实际代码:

if counter <= 59.0 {
            resultTimeLabel.text = String(format: "%.1", counter)
            resultTimeLabel.text = "in \(counter) seconds"
        }
        if counter >= 60.0 {
            let minutes = counter / 60.0
            resultTimeLabel.text = String(format: "%.1", counter)
            resultTimeLabel.text = "in \(minutes) minutes"

哪种格式使它看起来像:1.5 而不是 1.5000000023

这里的问题是您正在为之前的格式化字符串分配一个新字符串,并且它在您的字符串格式中缺少一个“f”:

if counter < 60 {
    resultTimeLabel.text = "in " + String(format: "%.1f", counter) + " seconds"
}
else 
if counter >= 60 {
    let minutes = counter / 60.0
    resultTimeLabel.text = "in " + String(format: "%.1f", minutes) + " minutes"