【问题标题】:How can I change time format of a label ? Swift如何更改标签的时间格式?迅速
【发布时间】:2020-11-14 16:34:08
【问题描述】:

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

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

【问题讨论】:

标签: swift time timer uilabel


【解决方案1】:

问题是您在之前的格式化字符串上分配了一个新字符串,并且在您的字符串格式中缺少一个“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"

【讨论】:

    猜你喜欢
    • 2021-08-09
    • 2015-04-23
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    相关资源
    最近更新 更多