在计时器中显示前导零 xcode

Displaying leading zeroes in a timer xcode

我想在我的应用程序中显示倒计时。计时器工作正常,但我希望它显示时间为 00:00:00。通常不会显示单个数字整数上的前导零(例如 0:1:15),但我无法弄清楚如何在不求助于笨拙的 if 语句的情况下将它们放入。这是相关代码:

-(void)timerFireMethod: (NSTimer *)timer {

   [self setUpTimer];
   self.timeLabel.text = [NSString stringWithFormat:@"%lu:%lu:%lu", self.timerParts.hour, self.timerParts.minute, self.timerParts.second];

   self.counter--; }

有谁知道我可以做些什么来显示前导零,以便 0:1:15 变成 00:01:15?

您可以通过添加 .2.
强制显示前导零 它始终显示两位数字,以防小于 10 且前导零。

self.timeLabel.text = [NSString stringWithFormat:@"%.2lu:%.2lu:%.2lu", self.timerParts.hour, self.timerParts.minute, self.timerParts.second];