我试图在Objective-C中编写这个快捷的行。
夫特
timeRemainingLabel.text = String(format: "%02d:%02d", ((lround(timeRemaining) / 60) % 60), lround(timeRemaining) % 60)
目标C
timeRemainingLabel.text = [NSString stringWithFormat:@"%02d:%02d", ((roundf(timeRemaining) / 60) % 60), roundf(timeRemaining) % 60];
但它给出了这个错误:
二进制表达式的操作数无效错误。('浮动'浮动')
如何解决这个问题?
答案 0 :(得分:0)
您需要将roundf
的结果转换为整数。
你的行看起来应该是这样的
timeRemainingLabel.text = [NSString stringWithFormat:@"%02d:%02d", (((int)roundf(timeRemaining)/ 60)%60),(int)roundf(timeRemaining)% 60];
编辑:评论中的工作答案:
CGFloat timeRemaining = 109.13;
NSString *string = [NSString stringWithFormat:@"%02d:%02d", ((int)(roundf(timeRemaining) / 60) % 60), ((int)roundf(timeRemaining) % 60)];
NSLog(@"String = %@", string);
答案 1 :(得分:0)
try dividing with decimal value. i.e. **/60.0f** instead of **/60**
timeRemainingLabel.text = [NSString stringWithFormat:@"%02d:%02d", ((roundf(timeRemaining) / 60) % 60), roundf(timeRemaining) % 60];
答案 2 :(得分:0)
试试这个:
CGFloat timeRemaining = 109.13;
NSString *string = [NSString stringWithFormat:@"%02d:%02d", ((int)(roundf(timeRemaining) / 60) % 60), ((int)roundf(timeRemaining) % 60)];
NSLog(@"String = %@", string);