ios宏

2020-09-07  本文已影响0人  我不白先生

1.宏

1.1宏是在编译之前,对源程序进行的一种预处理工作

1.2作用

1.2.1见名知意:是的数字变单词
1.2.2批量修改

1.3分类

1.3.1无参宏(宏常量)
1.3.2带参宏(宏函数)

#import "ViewController.h"
#define C_LANGUAGE_SCORE 98//定义了一个宏名
#define PI 3.14
#define 整型 int
#define TRMIN(x,y) ((x)<(y)?(x):(y))//因为MIN已经被系统定义过所以加了个公司前缀TR
#define IS_ODD(X) ((X)%2!=0)
#define LOWER(ch) ((ch)>='A'&&(ch)<='Z'?(ch)+32:(ch))

//做练习:在两个数中求大的一个、判断偶数、小写做大写
#define TRMAX(x,y) ((x)>(y)?(x):(y))//宏名都要写成大写
#define IS_EVEN(x) ((x)%2==0)
#define UPPER(bh) ((bh)>='a'&&(bh)<='z'?(bh)-32:(bh))
-(void)method1
{
    self.outputLabel.text = [NSString stringWithFormat:@"%d",C_LANGUAGE_SCORE];
//    整型 C语言成绩 = 98;
}

-(void)method2
{
    self.outputLabel.text = [NSString stringWithFormat:@"%d", TRMIN(3, 5)];
    int a = 5;
    self.outputLabel.text = [NSString stringWithFormat:@"%d%@奇数",a,IS_ODD(a)? @"是" : @"不是"];
    char c = 'D';
    self.outputLabel.text = [NSString stringWithFormat:@"%c",LOWER(c)];
    self.outputLabel.text = [NSString stringWithFormat:@"%d", TRMAX(3, 5)];
    int b = 6;
    self.outputLabel.text = [NSString stringWithFormat:@"%d%@偶数",b,IS_EVEN(b) ? @"是" : @"不是"];
    char e = 'd';
    self.outputLabel.text = [NSString stringWithFormat:@"%c",UPPER(e)];
}

1.宏
1.1带参宏(续)

#define PRINT(x,y) self.outputLabel.text=[NSString stringWithFormat:@#x"=%d,"@#y"=%d",x,y];

1.2带参宏注意事项
1.2.1带参宏中的参数一定要用括号括起来,以防替换后的优先级问题

1.2.2带参宏中的整个表达式也要用括号括起来,以防止替换后的优先级问题
1.2.3带参宏中有定义变量语句时,应该用大括号括起来形成复合语句,以防止变量的重定义问题。
1.2.4调用带参宏时,不要使用++、--

上一篇 下一篇

猜你喜欢

热点阅读