美文网首页
宏定义和宏方法

宏定义和宏方法

作者: iOS收藏家 | 来源:发表于2016-05-25 11:31 被阅读2364次

    宏定义和宏方法

    定义机型

    #define is_iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
    
    #define is_iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
    
    #define is_iPhone6P ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
    

    定义手机系统型号

    #define kOSVersion [[UIDevice currentDevice].systemVersion floatValue]
    #define kOS_iOS7 (OSVersion >= 7)
    #define kOS_iOS8 (OSVersion >= 8)   
    #define kOS_iOS9 (OSVersion >= 9)
    

    宏定义屏幕宽度和高度

    #define KScreenSize    [UIScreen mainScreen].bounds.size    
    #define KScreenHeight  ([[UIScreen mainScreen] bounds].size.height)
    #define KScreenWidth   ([[UIScreen mainScreen] bounds].size.width)
    

    定义打印的东西

    #define DLog(fmt, ...) NSLog((@"[文件名:%s]\n" "[函数名:%s]\n" "[行号:%d] \n" fmt), __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);
    

    通过RGB获得一个颜色

    static inline UIColor * getBackGoundColor(CGFloat r,CGFloat g,CGFloat b,CGFloat alpha){
    
    return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:alpha];
    
    }
    

    验证手机号

    static inline bool predicateIsMobilePhone(NSString * mobilePhone){
    
    NSString * validateContent = @"1[3|5|7|8|4|][0-9]{9}";
    
    NSPredicate *validateTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", validateContent];
    
    return [validateTest evaluateWithObject:mobilePhone];
    
    }
    

    比较大小和取绝对值

    #define MIN(A,B)    ((A) < (B) ? (A) : (B))
    
    #define MAX(A,B)    ((A) > (B) ? (A) : (B))
    
    #define ABS(A)  ((A) < 0 ? (-(A)) : (A))
    

    隐藏方法警告的方法

    #define SuppressPerformSelectorLeakWarning(Stuff) \
    do { \
    _Pragma("clang diagnostic push") \
    _Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
    Stuff; \
    _Pragma("clang diagnostic pop") \
    } while (0)
    

    添加观察者的宏定义方法

        #define REGISTER_SELECTOR(sel, notif_name)                  \
        if([obj respondsToSelector:sel])                            \
            [[NSNotificationCenter defaultCenter]   addObserver:obj \
                                                    selector:sel    \
                                                    name:notif_name \
                                                    object:nil      \
            ];  
    

    验证是不是表情
    #define kChat_String_EmojiRegex @"\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]"


    下面的宏定义方法在SDWebImage中定义
    不在主线程执行block

    #define dispatch_main_sync_safe(block)\
        if ([NSThread isMainThread]) {\
            block();\
        } else {\
            dispatch_sync(dispatch_get_main_queue(), block);\
        }
    

    在主线程去执行block

        #define dispatch_main_async_safe(block)\
        if ([NSThread isMainThread]) {\
            block();\
        } else {\
            dispatch_async(dispatch_get_main_queue(), block);\
        }
    

    参考地址

    相关文章

      网友评论

          本文标题:宏定义和宏方法

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