【发布时间】:2012-09-13 02:40:38
【问题描述】:
以下所有值都是双精度值,但 switch 需要一个整数值。反正有这个吗?
switch(fivePercentValue){
case floor((5*fivePercentValue) / 100):
fivePercent_.backgroundColor = [UIColor greenColor];
fivePercentLabel_.textColor = [UIColor greenColor];
break;
case ceil((5*fivePercentValue) / 100):
fivePercent_.backgroundColor = [UIColor greenColor];
fivePercentLabel_.textColor = [UIColor greenColor];
break;
default:
fivePercent_.backgroundColor = [UIColor redColor];
fivePercentLabel_.textColor = [UIColor redColor];
break;
【问题讨论】:
-
比较浮点的相等性从来都不是一个好主意,所以
switch在浮点上是一个非常糟糕的主意。 -
case值必须是编译时常量。
标签: objective-c ios switch-statement