The Weekend Blog

I hope it will continue to be updated

预编译指令

预编译指令

以#开头的都是预编译指令,就是在正式编译之前,编译器做一些预处理的工作

预编译指令 含义
#define 定义一个预处理宏
#undef 取消宏的定义
#if 编译预处理中的条件命令,相当于if语句
#ifdef 判断某个宏是否被定义,若已定义,执行随后的语句
#ifndef 与#ifdef相反,判断某个宏是否未被定义
#elif 若#if, #ifdef, #ifndef或前面的#elif条件不满足,则执行#elif之后的语句,相当于else-if
#else 与#if, #ifdef, #ifndef对应, 若这些条件不满足,则执行#else之后的语句,相当于else
#endif #if, #ifdef, #ifndef这些条件命令的结束标志.
defined 与#if, #elif配合使用,判断某个宏是否被定义

代码举例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 宏定义
#define ISOK
#define a 4
#define b 5
//取消宏定义
//#undef ISOK

// #if后面必须跟一个判断条件,如果需要判断是否有某个宏定义需要使用defined
#if defined(ISOK)  //if
    NSLog(@"#if==true");
#elif (a==b)  // else if
    NSLog(@"#elif==true");
#else // else 
    NSLog(@"#else==true");
#endif

    
#ifdef ISOK 
    NSLog(@"#ifdef:ISOK已有宏定义");
#else 
    NSLog(@"#ifdef:ISOK没有宏定义");
#endif
  
#ifndef ISOK
    NSLog(@"#ifndef:ISOK没有宏定义");
#else
    NSLog(@"#ifndef:ISOK已有宏定义");
#endif

一般在iOS中用到预编译指令的地方:

1.调试代码:

1
2
3
#ifdef DEBUG
// 调试代码 ,注意DEBUG宏是系统定义好的
#endif

2.统一定义设备相关信息

1
2
3
4
5
6
7
8
9
#define NavigationBar_HEIGHT 44

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

#define IOS_VERSION [[UIDevice currentDevice] systemVersion] floatValue]

#define CurrentSystemVersion [UIDevice currentDevice] systemVersion]

3.统一定义某个全局方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
 *    @brief  正确获取主线程
 */
#ifndef dispatch_main_async_safe
    #define dispatch_main_async_safe(block)\
        if (strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(dispatch_get_main_queue())) == 0) {\
            (block());\
        } else {\
            dispatch_async(dispatch_get_main_queue(), block);\
        }
#endif


/**
 *    @brief  在Debug模式显示日志,Release模式关闭日志,普通输出
 */
#ifdef DEBUG
#define Log( s, ... ) printf( "<%s:(%d)> %s %s\n\n", [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] cStringUsingEncoding:NSUTF8StringEncoding], __LINE__,__func__, [[NSString stringWithFormat:(s), ##__VA_ARGS__] cStringUsingEncoding:NSUTF8StringEncoding])
#else
#define Log( s, ... )
#endif

/**
 *    @brief  防止block循环引用
 */
#ifndef weakify
    #if TARGET_IPHONE_DEBUG
        #if __has_feature(objc_arc)
            #define weakify(object) @autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
        #else
            #define weakify(object) @autoreleasepool{} __block __typeof__(object) block##_##object = object;
        #endif
    #else
        #if __has_feature(objc_arc)
            #define weakify(object) @try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
        #else
            #define weakify(object) @try{} @finally{} {} __block __typeof__(object) block##_##object = object;
        #endif
    #endif
#endif

#ifndef strongify
    #if TARGET_IPHONE_DEBUG
        #if __has_feature(objc_arc)
            #define strongify(object) @autoreleasepool{} __typeof__(object) object = weak##_##object;
        #else
            #define strongify(object) @autoreleasepool{} __typeof__(object) object = block##_##object;
        #endif
    #else
        #if __has_feature(objc_arc)
            #define strongify(object) @try{} @finally{} __typeof__(object) object = weak##_##object;
        #else
            #define strongify(object) @try{} @finally{} __typeof__(object) object = block##_##object;
        #endif
    #endif
#endif

/**
 *    @brief rgb颜色转换(16进制->10进制)
 */
 #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]

/**
 *    @brief 通知
 */
#define POST_NOTIFY(__NAME, __OBJ, __INFO) [[NSNotificationCenter defaultCenter] postNotificationName:__NAME object:__OBJ userInfo:__INFO];

#define LISTEN_NOTIFY(__NAME, __OBSERVER, __SELECTOR) [[NSNotificationCenter defaultCenter] addObserver:__OBSERVER selector:__SELECTOR name:__NAME object:nil];

#define REMOVE_NOTIFY(__OBSERVER) [[NSNotificationCenter defaultCenter] removeObserver:__OBSERVER];