将 UIColor 值分配给宏
Assigning UIColor values to macros
这个问题是指这个SO post:How to set global variable with UIColor class
接受的答案建议像这样定义一个宏:
AppDelegate header:
// AppDelegate.h
#import <UIKit/UIKit.h>
#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]
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@end
ViewController 实施:
// ViewController.m
#import "AppDelegate.h"
#import "ViewController.h"
@implementation ViewController
- (void) viewDidLoad {
[super viewDidLoad];
...
// they both give the same error,
// with or without semi-colons
// self.view.backgroundColor = black;
self.view.backgroundColor = myColor;
}
@end
但是,Xcode 不断弹出这两个异常:
预期表达式
表达式不可赋值
我看过其他使用方法的建议,但这种方法最符合我的要求。
可能是什么原因造成的?
试试这个,这对我有用....
#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]
在此上下文中要理解的关键是预处理器宏仅 替换 (实际上,1 对 1 替换 )编译时宏的代码,因此你的错误生成代码实际上对编译器来说是这样的:
self.view.backgroundColor = = [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]
然后替换宏名称 myColor
实际上向编译器提供了所需的结果:
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);
这个问题是指这个SO post:How to set global variable with UIColor class
接受的答案建议像这样定义一个宏:
AppDelegate header:
// AppDelegate.h
#import <UIKit/UIKit.h>
#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]
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@end
ViewController 实施:
// ViewController.m
#import "AppDelegate.h"
#import "ViewController.h"
@implementation ViewController
- (void) viewDidLoad {
[super viewDidLoad];
...
// they both give the same error,
// with or without semi-colons
// self.view.backgroundColor = black;
self.view.backgroundColor = myColor;
}
@end
但是,Xcode 不断弹出这两个异常:
预期表达式
表达式不可赋值
我看过其他使用方法的建议,但这种方法最符合我的要求。
可能是什么原因造成的?
试试这个,这对我有用....
#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]
在此上下文中要理解的关键是预处理器宏仅 替换 (实际上,1 对 1 替换 )编译时宏的代码,因此你的错误生成代码实际上对编译器来说是这样的:
self.view.backgroundColor = = [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]
然后替换宏名称 myColor
实际上向编译器提供了所需的结果:
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);