OC 宏和预编译

来源:互联网 发布:gotomycloud软件安全吗 编辑:程序博客网 时间:2024/06/16 08:54

#import <Foundation/Foundation.h>

#define PI 3.1415926

#define TOW_PI PI*2

#define NO_CHESS "+"

#define GIRTH(r) (PI*2*(r))

#define AREA(r) (PI*(r)*(r))

//宏的参数一定要括号起来

#define iPad

#define AGE 20

//#undef PI//结束宏的范围

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        NSLog(@"%f",TOW_PI);

        NSLog(@"%f",GIRTH(3));

        NSLog(@"%f",AREA(3));


//执行条件编译

//定义指定的宏,则执行#ifdef #else之间的语言

#ifdef iPad //(不可以跟表达式)

        NSLog(@"iPad");

#else

         NSLog(@"iPone");

#endif


//没有定义指定的宏,则执行#ifndef iPad之间的语言

#ifndef iPad

        NSLog(@"iPad");

#else

        NSLog(@"iPone");

#endif

    }


//#使用#if、#elif、#else、#endif执行条件编译

//可以对指定的表达式进行判断,根据表达式的值决定是否要编译指定的语句。


#if AGE>20//(可以跟表达式)

    NSLog(@"20");

#elif AGE>21

    NSLog(@"21");

#elif AGE>24

    NSLog(@"24");

#else

    NSLog(@"25");

#endif

    return 0;

}