PCH文件设置以及常用的宏
来源:互联网 发布:淘宝的微淘怎么弄2016 编辑:程序博客网 时间:2024/06/11 14:21
师弟问题之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]
0 0
- PCH文件设置以及常用的宏
- pch文件常用的宏
- 常用的PCH文件
- pch文件的设置
- iOS开发:pch文件的使用,以及pch相对路径和绝对路径的设置方法
- IOS pch 文件的设置
- pch文件的使用, 头文件使用, 常量(const)的定义,以及一些常用的宏定义
- 给工程添加pch文件以及pch文件的作用
- iOS通过宏定义设置NSLog打印出类名、方法名和NSLog所在行数以及PCH文件的使用
- ios pch文件内 常用的各类宏定义
- iOS开发 Xcode添加PCH文件和常用的宏
- ios PCH文件的使用以及作用
- iOS开发之 pch文件的使用,以及相对路径和绝对路径的设置
- pch文件的创建与设置
- iOS 【.pch文件的路径方便设置】
- iOS设置PCH文件的相对路径
- xcode8 设置pch文件的方法
- 【iOS】设置pch文件的相对路径
- EditText和软键盘的暧昧
- 搜索引擎/网络爬虫程序源代码
- 海康IPC+ffmpeg+nginx+ckplayer实现网页实时预览监控视频
- Qt 的程序使用 Sleep
- python程序-->Collatz 序列问题
- PCH文件设置以及常用的宏
- HTTPS 与 HTTP2 协议分析
- Android——多线程(AsyncTask封装)
- asp.net mvc 中对easyui的引用文件使用Bundle的注意事项
- 从C#中的ArrayList和List的区别说起
- python sorted()
- 安卓学习笔记---Android图片加载框架最全解析(一),Glide的基本用法
- java日期
- 数学公式的英文读法