美文网首页iOS平时收藏算法或者代码
iOS开发pch结合headerFile的使用以及常用的宏定义

iOS开发pch结合headerFile的使用以及常用的宏定义

作者: 小二同學 | 来源:发表于2016-09-22 17:50 被阅读1684次

    在开发iOS时候,要经常需要用到宏定义,和一大堆接口,这时候就需要创建pch和headerFile了。

    • 首先要创建一个pch文件

    1.png
    • 记得这里打钩

    2.png
    • 然后配置pch路径

    3.png
    • 运行保证没有错,接下来创建headerFile

    4.png
    • 然后引入头文件

    5.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
    

    好啦,常用的就这么多了,以后有什么实用的东西我会再拿出来分享的。

    相关文章

      网友评论

      • 你好牛:在pct里引入header头文件报错 没有这个文件
      • 叶秋主:header file中使用static NSString *const VOICEFOLDER = @"voice";报错unknown type name "NSString",怎么回事啊
        小二同學:@叶秋主 #define VOICEFOLDER @"voice" 这样写
        叶秋主:@小二同學 用了,就是header file文件里报的错
        小二同學:头文件用了么
      • feng_dev:如果只用header file 怎么弄? pch Apple 不是不推荐 使用了吗
        小二同學:加入头文件 就行了

      本文标题:iOS开发pch结合headerFile的使用以及常用的宏定义

      本文链接:https://www.haomeiwen.com/subject/mubzettx.html