步骤:
1、创建一个pch文件
2、在pch文件中定义日志输出的宏
3、配置pch文件
comman + N 选中other 中的 PCH file next,会默认带有PrefixHeader这个名字,意思是预编译的头文件;
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 --> 输入路径 编译后便能使用
4、宏定义的debug /release切换,见下图操作步骤
在选择了Edit Scheme后 run info中切换debug/release模式