前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >编码篇-学会小用宏和条件编译

编码篇-学会小用宏和条件编译

作者头像
進无尽
发布2018-09-12 18:12:16
7260
发布2018-09-12 18:12:16
举报
文章被收录于专栏:進无尽的文章進无尽的文章

前言

宏定义在C系开发中可以说占有举足轻重的作用。底层框架自不必说,为了编译优化和方便,以及跨平台能力,宏被大量使用,可以说底层开发离开define将寸步难行。而在更高层级进行开发时,我们会将更多的重心放在业务逻辑上,似乎对宏的使用和依赖并不多。

编译时编译器会在语义分析认定是宏后,将形参替换为实参,这个过程称为宏的展开。

使用宏的好处:

  • 在节省工作量的同时,代码可读性大大增加。如打印语句可以使打印出来的内容更美观。
  • 写出漂亮优雅的代码(虽然宏本身可能并不漂亮优雅)。
  • 我们使用宏一是为了多处使用方便,而是方便修改:一处修改全局适用。

宏的分类使用

一般宏分为两类

对象宏(object-like macro)和函数宏(function-like macro)。

  • 对象宏:对于对象宏来说确实相对简单,一般用来定义一些常数。
  • 函数宏:函数宏顾名思义,就是行为类似函数,可以接受参数的宏。 在书写函数宏的时候注意多加(),这样可以避免优先级所造成的问题

一般开发中使用的宏大体为:

  • 颜色
  • 服务器地址
  • 储存取值
  • 屏幕宽高
  • 导航高度
  • 缩放比例
  • 其他常用的代码段

常用的宏定义(不全,但是以此类推)

代码语言:javascript
复制
#define ScreenWidth  [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height

#define NavBarHeight 64.0f
#define TabBarHeight 49.0f

#define selfWidth     self.frame.size.width
#define selfHeight    self.frame.size.height

#define ViewBorderRadius(View,Radius,Width,Color)    [View.layer setCornerRadius:(Radius)];\
                                                       [View.layer setMasksToBounds:YES];\
                                                       [View.layer setBorderWidth:(Width)];\
                                                       [View.layer setBorderColor:[Color CGColor]];//设置圆角

weak弱引用self

代码语言:javascript
复制
  #define WeakSelf __weak typeof(self) weakSelf = self;

weak弱引用对象

代码语言:javascript
复制
  #define WeakObj(obj) __weak typeof(obj) weakObj = obj;

strong强引用self

代码语言:javascript
复制
  #define StrongSelf(weakSelf) __strong typeof(weakSelf) strongSelf = weakSelf;

strong强引用对象

代码语言:javascript
复制
  #define StrongObj(obj) @autoreleasepool{} __strong typeof(obj) obj = strongObj;


  #define IS_IOS8       ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)

  //判断是不是iPad
  #define IS_iPad        UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
  //判断是不是iphone
  #define IS_IPHONE      [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone


  #define GetImage(imageName)     [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]

  #define NEWSTITLECOLOR ([UIColor colorWithRed:64/255.0 green:56/255.0 blue:53/255.0 alpha:1.0f])

  #define showMessage(a)   [SVProgressHUD showInfoWithStatus:a maskType:SVProgressHUDMaskTypeGradient];
  #define showSuccessMessage(a)  [SVProgressHUD showSuccessWithStatus:a maskType:SVProgressHUDMaskTypeGradient];

  #define SandboxPathStr [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
  #define cafPathStr     [SandboxPathStr stringByAppendingPathComponent:@"myRecord.caf"]
  #define mp3PathStr     [SandboxPathStr stringByAppendingPathComponent:@"myRecord.mp3"]

字符串是否为空

代码语言:javascript
复制
  #define LMStringIsEmpty(str)   ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )

数组是否为空

代码语言:javascript
复制
  #define LMArrayIsEmpty(array)  (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)

字典是否为空

代码语言:javascript
复制
  #define LMDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)

  #define LMApplication        [UIApplication sharedApplication]
  #define LMKeyWindow          [UIApplication sharedApplication].keyWindow

  #define LMAppVersion         [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]

property属性快速声明

代码语言:javascript
复制
#define PropertyString(s)                @property(nonatomic,copy)NSString    * s   
#define PropertyNSInteger(s)             @property(nonatomic,assign)NSInteger  s
#define PropertyFloat(s)                 @property(nonatomic,assign)float  s
#define   propertyStrong(a,b)            @property (strong, nonatomic) a *b;

DEBUG模式下打印日志,当前行

代码语言:javascript
复制
#ifdef DEBUG
    #define DLog(fmt,...)NSLog((@"%s[Line %d]" fmt),__PRETTY_FUNCTION__,__LINE__,##__VA_ARGS__);
#else
    #define DLog(...)
#endif

传参view对象,获取view的frame、bounds相关属性值

代码语言:javascript
复制
#define VIEW_BOUNDS(aView)       ((aView).bounds)

#define VIEW_FRAME(aView)        ((aView).frame)

#define VIEW_ORIGIN(aView)       ((aView).frame.origin)
#define VIEW_X(aView)            ((aView).frame.origin.x) 
#define VIEW_Y(aView)            ((aView).frame.origin.y)

#define VIEW_SIZE(aView)         ((aView).frame.size)
#define VIEW_HEIGHT(aView)       ((aView).frame.size.height)  // 视图高度
#define VIEW_WIDTH(aView)        ((aView).frame.size.width)   // 视图宽度

获取iPhone屏幕尺寸

代码语言:javascript
复制
// x
#define IS_iPhoneX        ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
// 6P、6sP、7P、8P
#define IS_iPhone678_Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
// 6、6s、7、8
#define IS_iPhone678      ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
// 5、5s
#define IS_iPhone5        ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
// 4、4s
#define IS_iPhone4       ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)

大小屏字体自动切换

有的应用希望有一个好的用户体验会在不同的屏幕上适配不同大小字体,这时就可以使用以下的宏定义来实现。但是如果应用中字体大小不能做到全局统一,就不要使用以下的宏定义来实现字体大小适配。例如:

代码语言:javascript
复制
#define IS_SmallScreen (IS_iPhone5 || IS_iPhone4)

#define MaxFontSize    (IS_SmallScreen ? 21.f : 25.f )
#define LagerFontSize  (IS_SmallScreen ? 17.f : 19.f )
#define BigFontSize    (IS_SmallScreen ? 15.f : 17.f )
#define NormalFontSize (IS_SmallScreen ? 13.f : 15.f )
#define SmallFontSize  (IS_SmallScreen ? 11.f : 13.f )
#define MinFontSize    (IS_SmallScreen ? 9.f  : 11.f )

校验相关

代码语言:javascript
复制
#define IsCanUseString(str)     ((str != nil) && ![str isKindOfClass:[NSNull class]] && [str isKindOfClass:[NSString class]] && [str length] > 0 )
#define IsCanUseArray(arr)      ( arr && (arr != nil) && ![arr isKindOfClass:[NSNull class]] )
#define IsCanUseDic(dic)        ( dic && (dic != nil) && ![dic isKindOfClass:[NSNull class]] )
#define IsCanUseObj(obj)        ( obj && (obj != nil) && ![obj isKindOfClass:[NSNull class]] )
#define IsNullClass(class)      [class isKindOfClass:[NSNull class]]

打印相关

这样的打印语句,省事而且美观易读
代码语言:javascript
复制
// mark(NSString类型参数)为打印内容标题

#define NSLOG_Str(mark,str)       NSLog(@"##%@##--str:%@--",(mark),(str))
#define NSLOG_Int(mark,int)       NSLog(@"##%@##--int:%ld--",(mark),(int))
#define NSLOG_Float(mark,float)   NSLog(@"##%@##--float:%f--",(mark),(float))
#define NSLOG_Bool(mark,bool)     NSLog(@"##%@##--bool:%@--",(mark),(bool) ? @"YES" : @"NO")
#define NSLOG_Point(mark,point)   NSLog(@"##%@##--x:%f--y:%f--",(mark),(point).x,(point).y)
#define NSLOG_Size(mark,size)     NSLog(@"##%@##--width:%f--height:%f--",(mark),(size).width,(size).height)
#define NSLOG_Frame(mark,frame)   NSLog(@"##%@##--x:%f--y:%f--width:%f--height:%f--",(mark),(frame).origin.x,(frame).origin.y,(frame).size.width,(frame).size.height)

条件编译

一般情况下,源程序中所有的行都参加编译。但是有时希望对其中一部分内容只在满足一定条件才进行编译,也就是对一部分内容指定编译的条件,这就是条件编译(不被编译的代码不会被运行)

条件编译语法格式

1、#if 编译预处理中的条件命令, 相当于C语法中的if语句

2、#ifdef 判断某个宏是否被定义, 若已定义, 执行随后的语句

3、#ifndef 与#ifdef相反, 判断某个宏是否未被定义

4、#elif 若#if, #ifdef, #ifndef或前面的#elif条件不满足, 则执行#elif之后的语句, 相当于C语法中的else-if

5、#else 与#if, #ifdef, #ifndef对应, 若这些条件不满足, 则执行#else之后的语句, 相当于C语法中的else

6、#endif #if, #ifdef, #ifndef这些条件命令的结束标志.

7、#if 与 #ifdef 的区别:#if是判断后面的条件语句是否成立,#ifdef是判断某个宏是否被定义过。要区分开!

代码语言:javascript
复制
 #if 条件1
 ...code1...
 #elif 条件2
  ...code2...
 #else
 ...code3...
 #endif
 *******************
  #ifdef 标识符    // 如果定义了 标识符
    程序段1
 #else
     程序段2
 #endif
 它的作用是:当标识符已经被定义过(一般是用#define命令定义),则对程序段1进行编译,否则编译程序段2。其中#else部分也可以没有,即:
  #ifdef
      程序段1
  #denif
*******************
  #ifndef MACRO_Define // 如果未定义MACRO_Define这个宏
      代码块1
  #else
      代码块2
  #endif

说明:预处理指令是编译之前的,不是运行时的,所以条件编译时要注意if的条件,不要还没运行,就先用源程序里面的变量作为条件进行判断,变量是运行时才产生的,而条件编译呢是在运行之前编译的。所以条件编译的条件一般是利用宏定义,因为宏定义和条件编译都是编译之前进行的。

代码语言:javascript
复制
如下面的一个错误例子:

  #include<stdio.h>
   void main()
  {
      int a =8;
      #if a>7
          printf("a>7");
      #elif a<7
          printf("a<7");
    #else
        printf("a!=7");
    #endif
}
运行结果为a<7,与我们期望的结果不一样,正常应该是输出a>7才对。

正确的写法如下:

 #include<stdio.h>
 #define a 8
  void main()
  {
    #if a>7
        printf("a>7");
    #elif a<7
      printf("a<7,%d",a);
    #else
        printf("a!=7");
    #endif
  }
  输出结果为a>7
条件编译的使用

测试服务器、正式服务器的自动切换。

代码语言:javascript
复制
#ifdef DEBUG
    #define request11   @"123"
#else
    #define request11   @"4565"
#endif
如何设置环境变量配置进行条件编译
  • 通过 Configurations 添加多个环境
  • 不同环境下设置不同的宏定义(在某个环境下设置的宏只能在哪个环境下的 Targets 中被识别,否则会报错,所以建议不同环境下定义同一个全局变量为不同的值。)
  • 区分不同的环境 if (DEBUG==1) { NSLog(@"测试环境"); }else if (DEBUG==2){ NSLog(@"运营环境"); }else{ NSLog(@"生产环境"); }

我们可以通过设置多个环境,每一种环境下的值不同;

生成多个Scheme,每一个Scheme对应一种环境和配置;

这样切换Scheme 即可切换到不同的环境下。

感兴趣的可以查看我的另一篇文章:基础篇-工程管理之多Targets

小结

宏的使用,重点是函数宏的使用,后续有新的使用心得会持续更新本文。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.11.28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 使用宏的好处:
  • 宏的分类使用
  • 常用的宏定义(不全,但是以此类推)
    • 这样的打印语句,省事而且美观易读
    • 条件编译
      • 条件编译语法格式
        • 条件编译的使用
          • 如何设置环境变量配置进行条件编译
          • 小结
          相关产品与服务
          测试服务
          测试服务 WeTest 包括标准兼容测试、专家兼容测试、手游安全测试、远程调试等多款产品,服务于海量腾讯精品游戏,涵盖兼容测试、压力测试、性能测试、安全测试、远程调试等多个方向,立体化安全防护体系,保卫您的信息安全。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档