[英]Why is conditional operator giving desired result for first statement but not for second?
第二个是给一些天蓝色
#define GREEN_COLOR 0x69BC63
#define RED_COLOR 0xCC4C46
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
//Working
[amount setTextColor: [[trans amount] intValue]>=0 ? UIColorFromRGB(GREEN_COLOR):UIColorFromRGB(RED_COLOR)];
//Not working
[amount setTextColor: UIColorFromRGB([[trans amount] intValue]>=0 ? GREEN_COLOR:RED_COLOR)];
您有括号问题。
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
可以重做为
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)(((rgbValue) & 0xFF0000) >> 16))/255.0 green:((float)(((rgbValue) & 0xFF00) >> 8))/255.0 blue:((float)((rgbValue) & 0xFF))/255.0 alpha:1.0]
这应该可以解决您的问题,但会使宏更加难以阅读。
您确实应该考虑使它成为适当的功能,而不是宏。
至少要使用一条语句表达式,因此rgbValue
仅被评估一次。
#define UIColorFromRGB(rgbValue) ({ unsigned v = rgbValue; [UIColor colorWithRed:((float)((v & 0xFF0000) >> 16))/255.0 green:((float)((v & 0xFF00) >> 8))/255.0 blue:((float)(v & 0xFF))/255.0 alpha:1.0]; })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.