师弟问题之pch是什么?怎么用
一、pch简介
pch文件是一个标准的预编译头文件( Pre-Compiled Header). 在Xcode6之前,系统会在Supporting Files文件夹自动创建。但在Xcode6之后取消了这一文件,如果我们需要使用pch文件,则需要手动创建。
手动创建pch
1 .在Supporting Files中新建文件
New File–>Other–>PCH File
2 .选择pch文件
3 .命名为×××.pch
4 .配置路径
当Precompile Prefix Header为YES,那么pch会被预编译,预编译后的pch文件会被缓存起来,从而提高编译速度。当 Precompile Prefix Header 为NO时,那么pch不会被预编译,而是在每一个用到它导入的框架类库的.m文件中编译一次。
$(SRCROOT)代表你工程根目录,无需更改。后面分别是你的项目名称以及你的pch文件名
然后就可以在在pch中写一个宏定义,在其他文件中尽情的使用了
二、常用的宏定义
1.获取屏幕宽度与高度
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height
如果支持横屏可以用下面的宏:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 // 当前Xcode支持iOS8及以上
#define SCREEN_WIDTH ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width)
#define SCREENH_HEIGHT ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.height)
#define SCREEN_SIZE ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?CGSizeMake([UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale,[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale):[UIScreen mainScreen].bounds.size)
#else
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_SIZE [UIScreen mainScreen].bounds.size
#endif
2.获取通知中心
#define WMNotificationCenter [NSNotificationCenter defaultCenter]
3.设置随机颜色
#define WMRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
4.设置RGB颜色/设置RGBA颜色
#define WMRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define WMRGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]
// clear背景颜色
#define WMClearColor [UIColor clearColor]
5.自定义高效率的 NSLog
项目开发中,我们会在许多地方加上Log,但是发布的时候又不想用这些Log,我们也不可能一个一个的删除,所以自定义Log是必然的!
#ifdef DEBUG
#define LRLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define LRLog(...)
#endif
6.弱引用/强引用
#define WMWeakSelf(type) __weak typeof(type) weak##type = type;
#define WMStrongSelf(type) __strong typeof(type) type = weak##type;
使用方法.png
第二种使用方法,定义完弱引用宏之后,直接敲weak.png
7.设置 view 圆角和边框
#define WMViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]
8.由角度转换弧度 由弧度转换角度
#define WMDegreesToRadian(x) (M_PI * (x) / 180.0)
#define WMRadianToDegrees(radian) (radian*180.0)/(M_PI)
9.获取view的frame/图片资源
//获取view的frame(不建议使用)
//#define kGetViewWidth(view) view.frame.size.width
//#define kGetViewHeight(view) view.frame.size.height
//#define kGetViewX(view) view.frame.origin.x
//#define kGetViewY(view) view.frame.origin.y
10、获取图片资源
#define kGetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]
11.使用 ARC 和 MRC
#if __has_feature(objc_arc)
// ARC
#else
// MRC
#endif
12.判断当前的iPhone设备/系统版本
//判断是否为iPhone
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE ([[[UIDevice currentDevice] model] isEqualToString:@"iPhone"])
//判断是否为iPad
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPAD ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"])
//判断是否为ipod
#define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])
// 判断是否为 iPhone 5SE
#define iPhone5SE [[UIScreen mainScreen] bounds].size.width == 320.0f && [[UIScreen mainScreen] bounds].size.height == 568.0f
// 判断是否为iPhone 6/6s
#define iPhone6_6s [[UIScreen mainScreen] bounds].size.width == 375.0f && [[UIScreen mainScreen] bounds].size.height == 667.0f
// 判断是否为iPhone 6Plus/6sPlus
#define iPhone6Plus_6sPlus [[UIScreen mainScreen] bounds].size.width == 414.0f && [[UIScreen mainScreen] bounds].size.height == 736.0f
//获取系统版本
//这个方法不是特别靠谱
#define IOS_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
//建议使用这个方法
#define IOS_SYSTEM_STRING [[UIDevice currentDevice] systemVersion]
//判断 iOS 8 或更高的系统版本
#define IOS_VERSION_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0)? (YES):(NO))
13.判断是真机还是模拟器
// 判断是不是iOS系统,如果是iOS系统在真机和模拟器输出都是YES
#if TARGET_OS_IPHONE
#endif
#if (TARGET_IPHONE_SIMULATOR)
// 在模拟器的情况下
#else
// 在真机情况下
#endif
14.沙盒目录文件
//获取temp
#define kPathTemp NSTemporaryDirectory()
//获取沙盒 Document
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
//获取沙盒 Cache
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]