如何在swift 4中数字为00时避免一个数字?

how to avoid one number when the number is 00 in swift 4?

我在我的应用程序中使用了当前时间的方法但是问题是,当 0 位于数字后面时,应用程序不会像 10:23:4 那样打印,正如您在第二个是 04 时看到的那样不会打印 04 和打印 4 - 我知道我可以用 if else 方法处理这个问题但是我想知道是否可以在代码中修复它或者唯一的方法是吗?我知道我可以使用 for 循环,但我想使用一行代码来解决这个问题,因为如果使用 for 循环,我也需要使用它一小时和几分钟!

这是我的代码

let date = Date()
    let calendar = Calendar.current

    let hour = calendar.component(.hour, from: date)
    let minutes = calendar.component(.minute, from: date)
    let seconds = calendar.component(.second, from: date)

    print("Time=\(hour):\(minutes):\(seconds)")

试试这个!

let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let seconds = calendar.component(.second, from: date)
let hourString = String.init(format: " %02d:%02d:%02d", hour,minutes,seconds)
print(hourString)