[英]Invalid operand to binary expression Error
I'm trying to write this swift line in Objective-C. 我正在尝试在Objective-C中写这条快速线。
Swift 迅速
timeRemainingLabel.text = String(format: "%02d:%02d", ((lround(timeRemaining) / 60) % 60), lround(timeRemaining) % 60)
Objective-C Objective-C的
timeRemainingLabel.text = [NSString stringWithFormat:@"%02d:%02d", ((roundf(timeRemaining) / 60) % 60), roundf(timeRemaining) % 60];
But it gives this error: 但是它给出了这个错误:
Invalid operand to binary expression Error.('float' and 'float') 二进制表达式错误的操作数无效。(“ float”和“ float”)
How to fix this? 如何解决这个问题?
You need to cast the results of roundf
into integers. 您需要将roundf
的结果roundf
为整数。 Your line should look something like this 您的行应如下所示
timeRemainingLabel.text = [NSString stringWithFormat:@"%02d:%02d", (((int)roundf(timeRemaining) / 60) % 60), (int)roundf(timeRemaining) % 60]; timeRemainingLabel.text = [NSString stringWithFormat:@“%02d:%02d”,((((int)roundf(timeRemaining)/ 60)%60),(int)roundf(timeRemaining)%60];
Edit: The working answer from a comment: 编辑:从评论的工作答案:
CGFloat timeRemaining = 109.13;
NSString *string = [NSString stringWithFormat:@"%02d:%02d", ((int)(roundf(timeRemaining) / 60) % 60), ((int)roundf(timeRemaining) % 60)];
NSLog(@"String = %@", string);
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];
try this : 尝试这个 :
CGFloat timeRemaining = 109.13;
NSString *string = [NSString stringWithFormat:@"%02d:%02d", ((int)(roundf(timeRemaining) / 60) % 60), ((int)roundf(timeRemaining) % 60)];
NSLog(@"String = %@", string);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.