iOS开发pch结合headerFile的使用以及常用的宏定义
2016-09-22 本文已影响1684人
小二同學
在开发iOS时候,要经常需要用到宏定义,和一大堆接口,这时候就需要创建pch和headerFile了。
-
首先要创建一个pch文件
![](https://img.haomeiwen.com/i2119364/51b1e6a5be12255e.png)
-
记得这里打钩
![](https://img.haomeiwen.com/i2119364/7e9f70469eb5e9b9.png)
-
然后配置pch路径
![](https://img.haomeiwen.com/i2119364/5fb200ffc0ac7cef.png)
-
运行保证没有错,接下来创建headerFile
![](https://img.haomeiwen.com/i2119364/126752f114e24b0b.png)
-
然后引入头文件
![](https://img.haomeiwen.com/i2119364/9b49fb1fb502e79d.png)
-
这样两个文件就都能用了,可以一个方法宏定义,一个放接口,以至于项目管理起来不会太乱。
-
接下来,分享一些经常使用和比较有用的宏定义
判断字符串是否为空
#define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
判断数组是否为空
#define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
判断字典是否为空
#define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
判断是否是空对象
#define kObjectIsEmpty(_object) (_object == nil \
|| [_object isKindOfClass:[NSNull class]] \
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))
获取屏幕宽度与高度
#define kScreenWidth ([UIScreen mainScreen].bounds.size.width)
#define kScreenHeight ([UIScreen mainScreen].bounds.size.height)
一些缩写
#define kApplication [UIApplication sharedApplication]
#define kKeyWindow [UIApplication sharedApplication].keyWindow
#define kAppDelegate [UIApplication sharedApplication].delegate
#define kUserDefaults [NSUserDefaults standardUserDefaults]
#define kNotificationCenter [NSNotificationCenter defaultCenter]
APP版本号
#define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
手机型号
#define iPhone4S ([UIScreen mainScreen].bounds.size.height == 480)
#define iPhone5S ([UIScreen mainScreen].bounds.size.height == 568)
#define iPhone6S ([UIScreen mainScreen].bounds.size.height == 667)
#define iPhone6pS ([UIScreen mainScreen].bounds.size.height == 736)
归档 存储路径
#define SavePath ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"/user.txt"])
开发的时候打印,但是发布的时候不打印的NSLog
#ifdef DEBUG
#define NSLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define NSLog(...)
#endif
色值 可以直接使用rgb色值
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
设备的比例系数(适配特别好用)
#define ScaleNumberWidth kScreenWidth/320
#define ScaleNumberHeight kScreenHeight/568
#define CarveHeight 1