且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

秒表以 2 的幂计数

更新时间:2023-11-30 19:05:16

如果您在每次迭代时安排计时器,请不要使用 repeats:YES.

Don't use repeats:YES if you schedule the timer at every iteration.

您在每次迭代时生成一个计时器,而该计时器已经在重复,导致计时器呈指数级增长(从而导致对 秒表 的方法调用).

You're spawning one timer at every iteration and the timer is already repeating, resulting in an exponential growth of timers (and consequently of method calls to stopwatch).

将计时器实例更改为:

[NSTimer scheduledTimerWithTimeInterval:1.0f
                                 target:self
                               selector:@selector(stopwatch)
                               userInfo:nil
                                repeats:NO];

或在秒表方法外启动

对于第二个问题,只需使用正确的格式字符串.

For the second issue simply use a proper format string.

NSString *hourString = [NSString stringWithFormat:@"%02d", hourInt];
NSString *minuteString = [NSString stringWithFormat:@"%02d", minuteInt];
NSString *secondString = [NSString stringWithFormat:@"%02d", secondInt];

%02d 将打印一个十进制数,用 0s 填充它,直到长度为 2,这正是您想要的.

%02d will print a decimal number padding it with 0s up to length 2, which is precisely what you want.

(来源)