美文网首页#iOS#HeminWon
学习笔记-iOS常用宏定义

学习笔记-iOS常用宏定义

作者: Mystical | 来源:发表于2016-12-09 10:24 被阅读28次
    • 开发的时候打印,但是发布的时候不打印
    #ifdef DEBUG
      #define NSLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
    #else
    #define NSLog(...)
    #endif
    
    • 强/弱引用
     #define strongSelf(type) __strong typeof(type) type = weak##type;
     #define weakSelf(type)  __weak typeof(type) weak##type = type;
    
    • RGB颜色 16进制->10进制
    #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]
    // 使用示例
        self.view.backgroundColor = UIColorFromRGB(0xFFB6C1);
    
    • RGBA颜色
    #define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
    
    • 获取屏幕宽度、高度
    #define ScreenWidth  ([UIScreen mainScreen].applicationFrame.size.width)
    #define ScreenHeight ([UIScreen mainScreen].applicationFrame.size.height)
    
    • 获取Document路径
    #define DocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
    
    • 获取Temp路径
    #define TempPath NSTemporaryDirectory()
    
    • 获取Cache路径
    #define CachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
    
    • 字符串是否为空
    #define StringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
    
    • 数组是否为空
    #define ArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
    
    • 字典是否为空
    #define DictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
    
    • 对象是否为空
    #define ObjectIsEmpty(_object) (_object == nil \
      || [_object isKindOfClass:[NSNull class]] \
      || ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
      || ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))
    
    • 系统版本检测
     #define SYSTEM_VERSION_EQUAL_TO(v)                  ([[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
      #define SYSTEM_VERSION_GREATER_THAN(v)              ([[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
      #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
      #define SYSTEM_VERSION_LESS_THAN(v)                ([[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
      #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)    ([[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
    

    相关文章

      网友评论

        本文标题:学习笔记-iOS常用宏定义

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