IOS pch预编译的头文件使用,宏定义控制NSLog输出

步骤:

1、创建一个pch文件

2、在pch文件中定义日志输出的宏

3、配置pch文件

comman + N 选中other 中的 PCH file next,会默认带有PrefixHeader这个名字,意思是预编译的头文件;

fd32fde2025ed7a2d945b6154ff7abb03d9.jpg

PrefixHeader.pch文件的作用:是用来包含一些常用的头文件,比如一些工具类的头文件;这样就不用在使用的类中专门在引入头文件,然后再调用静态方法了;

比如我们定义一个HQUtils的工具类,提供一个add'静态方法,然后再PrefixHeader文件中引用,同事通过宏来自定义只能在debug环境下使用输出日志,

 

//1、HQUtils工具类头文件:HQUtils.h文件
#import <Foundation/Foundation.h>

@interface HQUtils : NSObject

+(int) add:(int)a andb:(int)b; 
@end

//2、实现文件HQUtils.m文件

#import "HQUtils.h"

@implementation HQUtils

+(int) add:(int)a andb:(int)b {
    return a + b;
}
@end


//3、PrefixHeader.pch文件中

//用来包含一些常用的头文件

//用来包含一些常用的头文件
#ifdef __OBJC__

#import "HQUtils.h"

#ifdef DEBUG
# define HQLog(...) NSLog(__VA_ARGS__);
#else
# define HQLog(...);
#endif

#endif



//4、测试
-(void) testNSLog {
   int result =  [HQUtils add:1 andb:2];
 
    HQLog(@"%i", result);
    
    NSDictionary * dic = [NSBundle mainBundle].infoDictionary;
    HQLog(@"%@", dic);
}

//5、输出结果如下:
2018-06-29 23:51:05.156664+0800 ICEDemo[96829:4249965] 3
2018-06-29 23:51:05.157104+0800 ICEDemo[96829:4249965] {
    BuildMachineOSBuild = 17E199;
    CFBundleDevelopmentRegion = en;
    CFBundleExecutable = ICEDemo;
    CFBundleIdentifier = "cmcm.ICEDemo";
    CFBundleInfoDictionaryVersion = "6.0";
    CFBundleName = ICEDemo;
    CFBundleNumericVersion = 16809984;
    CFBundlePackageType = APPL;
    CFBundleShortVersionString = "1.0";
    CFBundleSupportedPlatforms =     (
        iPhoneSimulator
    );
    CFBundleVersion = 1;
    DTCompiler = "com.apple.compilers.llvm.clang.1_0";
    DTPlatformBuild = "";
    DTPlatformName = iphonesimulator;
    DTPlatformVersion = "11.4";
    DTSDKBuild = 15F79;
    DTSDKName = "iphonesimulator11.4";
    DTXcode = 0940;
    DTXcodeBuild = 9F1027a;
    LSRequiresIPhoneOS = 1;
    MinimumOSVersion = "11.4";
    UIDeviceFamily =     (
        1,
        2
    );
    UILaunchStoryboardName = LaunchScreen;
    UIMainStoryboardFile = Main;
    UIRequiredDeviceCapabilities =     (
        armv7
    );
    UISupportedInterfaceOrientations =     (
        UIInterfaceOrientationPortrait,
        UIInterfaceOrientationLandscapeLeft,
        UIInterfaceOrientationLandscapeRight
    );
}

//#ifdef __OBJC__  表示只在oc中使用

3、配置pch文件后才能使用

项目 Build Settings --> All --> 搜索Prefix Header  --> 输入路径 编译后便能使用

fd2a473a8ca2041eaca667d0dcd4a5de2cf.jpg

4、宏定义的debug /release切换,见下图操作步骤

在选择了Edit Scheme后 run info中切换debug/release模式

457bd937768e872c558899b82215f9e53f3.jpg

49e9af75245903567a54745188ae450c37c.jpg

转载于:https://my.oschina.net/goboy/blog/1838003

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值