Ios 将UIColor值指定给宏
这个问题涉及到这个SO职位: 公认的答案是这样定义宏: AppDelegate标题: 但是,Xcode不断弹出这两个异常: 预期表达式 表达式不可赋值 我见过其他使用方法的建议,但这种方法最适合我的要求Ios 将UIColor值指定给宏,ios,objective-c,macros,global-variables,uicolor,Ios,Objective C,Macros,Global Variables,Uicolor,这个问题涉及到这个SO职位: 公认的答案是这样定义宏: AppDelegate标题: 但是,Xcode不断弹出这两个异常: 预期表达式 表达式不可赋值 我见过其他使用方法的建议,但这种方法最适合我的要求 是什么原因造成的?试试这个,这对我有用 #define customBlueColour [UIColor colorWithRed:0.0/255.0 green:181.0/255.0 blue:237.0/255.0 alpha:1.0] 试试这个,这个对我有用 #define cust
是什么原因造成的?试试这个,这对我有用
#define customBlueColour [UIColor colorWithRed:0.0/255.0 green:181.0/255.0 blue:237.0/255.0 alpha:1.0]
试试这个,这个对我有用
#define customBlueColour [UIColor colorWithRed:0.0/255.0 green:181.0/255.0 blue:237.0/255.0 alpha:1.0]
用这个替换你的代码
#define black [UIColor blackColor]
#define myColor [UIColor colorWithRed:(238.0f/255.0f) green:(251.0f/255.0f) blue:(255.0f/255.0f) alpha:0.8f]
用这个替换你的代码
#define black [UIColor blackColor]
#define myColor [UIColor colorWithRed:(238.0f/255.0f) green:(251.0f/255.0f) blue:(255.0f/255.0f) alpha:0.8f]
在此上下文中需要理解的关键是,预处理器宏在编译时只替换宏的代码,因此在编译器看来,生成错误的代码实际上是这样的:
self.view.backgroundColor = = [UIColor colorWithRed:(238.0f/255.0f) green:(251.0f/255.0f) blue:(255.0f/255.0f) alpha:0.8f];
self.view.backgroundColor = [UIColor colorWithRed:(238.0f/255.0f) green:(251.0f/255.0f) blue:(255.0f/255.0f) alpha:0.8f]; // myColor has been replaced with the content of the macro
你可以亲眼看到这是没有道理的
定义要使用的宏时,请确保删除赋值运算符(=
):
#define black [UIColor blackColor]
#define myColor [UIColor colorWithRed:(238.0f/255.0f) green:(251.0f/255.0f) blue:(255.0f/255.0f) alpha:0.8f]
然后替换宏名myColor
实际上会向编译器提供所需的结果:
self.view.backgroundColor = = [UIColor colorWithRed:(238.0f/255.0f) green:(251.0f/255.0f) blue:(255.0f/255.0f) alpha:0.8f];
self.view.backgroundColor = [UIColor colorWithRed:(238.0f/255.0f) green:(251.0f/255.0f) blue:(255.0f/255.0f) alpha:0.8f]; // myColor has been replaced with the content of the macro
在此上下文中需要理解的关键是,预处理器宏在编译时只替换宏的代码,因此在编译器看来,生成错误的代码实际上是这样的:
self.view.backgroundColor = = [UIColor colorWithRed:(238.0f/255.0f) green:(251.0f/255.0f) blue:(255.0f/255.0f) alpha:0.8f];
self.view.backgroundColor = [UIColor colorWithRed:(238.0f/255.0f) green:(251.0f/255.0f) blue:(255.0f/255.0f) alpha:0.8f]; // myColor has been replaced with the content of the macro
你可以亲眼看到这是没有道理的
定义要使用的宏时,请确保删除赋值运算符(=
):
#define black [UIColor blackColor]
#define myColor [UIColor colorWithRed:(238.0f/255.0f) green:(251.0f/255.0f) blue:(255.0f/255.0f) alpha:0.8f]
然后替换宏名myColor
实际上会向编译器提供所需的结果:
self.view.backgroundColor = = [UIColor colorWithRed:(238.0f/255.0f) green:(251.0f/255.0f) blue:(255.0f/255.0f) alpha:0.8f];
self.view.backgroundColor = [UIColor colorWithRed:(238.0f/255.0f) green:(251.0f/255.0f) blue:(255.0f/255.0f) alpha:0.8f]; // myColor has been replaced with the content of the macro
可以为所有颜色创建单个全局宏
#define getColor(_r_, _g_, _b_, _a_) [UIColor colorWithRed:_r_/255.0 green:_g_/255.0 blue:_b_/255.0 alpha:_a_]
用户只需传递红-绿-蓝Alpha值即可随时随地使用
self.view.backgroundColor = getColor(238.0f, 251.0f, 255.0f , 1.0f);
可以为所有颜色创建单个全局宏
#define getColor(_r_, _g_, _b_, _a_) [UIColor colorWithRed:_r_/255.0 green:_g_/255.0 blue:_b_/255.0 alpha:_a_]
用户只需传递红-绿-蓝Alpha值即可随时随地使用
self.view.backgroundColor = getColor(238.0f, 251.0f, 255.0f , 1.0f);
可能重复@Yuyutsu:不完全是;这些解决方案利用了方法实现。我对常量定义感兴趣。可能重复@yutsu:不完全是;这些解决方案利用了方法实现。我对常数定义感兴趣。谢谢。不知道我怎么会错过,谢谢。我不知道我是怎么错过的。实际上是在我复制的原始帖子中。显然是我抄错了。谢谢你的回答。实际上是在我复制的原始帖子中。显然是我抄错了。谢谢你的回答。我在最初的帖子中,以及在我读到的所有其他关于宏定义的文档中,都没有提到这一点。也许是故意失明?谢谢你,伯克。选择这一点作为解释的答案。我在最初的帖子中,以及在我阅读的所有其他关于宏定义的文档中,都忽略了这一点。也许是故意失明?谢谢你,伯克。选择此作为解释的可接受答案。