• iOS开发常用宏定义


     

    //获取屏幕 宽度、高度
    #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
    #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
    
    // 根据版本号导入文件
    #ifdef NSFoundationVersionNumber_iOS_9_x_Max #import <UserNotifications/UserNotifications.h> #endif

      

    /**
     *  版本号
     *
     *  @param  CFBundleShortVersionString
     *
     *  @return kVersion
     */
    #define kVersion [NSString stringWithFormat:@"%@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]]
    

     

    /**
     *  iPhone or iPad
     */
    #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    #define IS_PAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    

      

    #define IPHONE4S ((SCREEN.height == 480.0f)?YES:NO)
    #define IPHONE5 ((SCREEN.height == 568.0f)?YES:NO)
    #define IPHONE6 ((SCREEN.height == 667.0f)?YES:NO)
    #define IPHONE6P ((SCREEN.height == 736.0f)?YES:NO)
    
    // 操作系统宏
    #define IOS10_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0)
    #define IOS9_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)
    #define IOS8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    #define IOS7_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    
    // 线程开启宏
    #define dispatch_main_async_withsafe(block)
    if ([NSThread isMainThread]) {
    block();
    } else {
    dispatch_async(dispatch_get_main_queue(), block);
    }
    
    #define dispatch_global_async_withsafe(block)
    if ([NSThread isMainThread]) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),block);
    } else {
    block();
    }

    单粒模式宏定义

    #pragma mark - 实例定义
    #define SingletenInterface 
    + (instancetype)sharedInstance;
    
    
    #pragma mark - 定义单例代码块
    #define SingletenImpl 
    static id _instace; 
    
    + (id)allocWithZone:(struct _NSZone *)zone 
    { 
        static dispatch_once_t onceToken; 
        dispatch_once(&onceToken, ^{ 
            _instace = [super allocWithZone:zone]; 
        }); 
        return _instace; 
    } 
    
    - (id)copyWithZone:(NSZone *)zone 
    { 
        return _instace; 
    } 
    
    + (instancetype)sharedInstance 
    { 
        static dispatch_once_t onceToken; 
        dispatch_once(&onceToken, ^{ 
            _instace = [[self alloc] init]; 
        }); 
        return _instace; 
    } 
    

     宏中使用##,传入参数作为shared方法名称:

    //
    //  Singleten.h
    //  可变参数测试
    //
    //  Created by HJiang on 2017/9/1.
    //  Copyright © 2017年 HJiang. All rights reserved.
    //
    
    #ifndef Singleten_h
    #define Singleten_h
    
    #pragma mark - 实例定义
    #define SingletenInterface(name) 
    + (instancetype)shared##name;
    
    
    #pragma mark - 定义单例代码块
    #define SingletenImpl(name) 
    static id _instace; 
    
    + (id)allocWithZone:(struct _NSZone *)zone 
    { 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
    _instace = [super allocWithZone:zone]; 
    }); 
    return _instace; 
    } 
    
    - (id)copyWithZone:(NSZone *)zone 
    { 
    return _instace; 
    } 
    
    + (instancetype)shared##name 
    { 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
    _instace = [[self alloc] init]; 
    }); 
    return _instace; 
    } 
    
    
    
    #endif /* Singleten_h */
    

    定义开发模式和发布模式条件宏

    #ifdef DEBUG
    #define CCDebugLog(fmt,...) 
    if ([[NSThread currentThread] isMainThread]) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSString *newFrt = [@"[文件名:%s]
     [函数名:%s]
     [行号:%d]
    " stringByAppendingString:fmt ];
    NSString *logContent = [NSString stringWithFormat:newFrt,__FILE__, __FUNCTION__, __LINE__,##__VA_ARGS__];
    logContent = [NSString stringWithFormat:@"%@%@%@",@"#####",logContent,@"#####"];
    NSLog(@"%@",logContent);
    });
    }else{
    NSString *newFrt = [@"[文件名:%s]
     [函数名:%s]
     [行号:%d]
    " stringByAppendingString:fmt ];
    NSString *logContent = [NSString stringWithFormat:newFrt,__FILE__, __FUNCTION__, __LINE__,##__VA_ARGS__];
    logContent = [NSString stringWithFormat:@"%@%@%@",@"#####",logContent,@"#####"];
    NSLog(@"%@",logContent);
    }
    
    #else
    #define CCDebugLog(fmt,...)
    # endif
    

    替换系统NSLog

    #ifdef DEBUG
    #define NSLog(format, ...) printf("
    [%s] %s [第%d行] %s
    ", __TIME__, __FUNCTION__, __LINE__, [[NSString stringWithFormat:format, ## __VA_ARGS__] UTF8String]);
    #else
    #define NSLog(format, ...)
    #endif
    
  • 相关阅读:
    Entity Framework Core 2.0 新特性
    asp.net core部署时自定义监听端口,提高部署的灵活性
    asp.net core使用jexus部署在linux无法正确 获取远程ip的解决办法
    使用xshell连接服务器,数字键盘无法使用解决办法
    使用Jexus 5.8.2在Centos下部署运行Asp.net core
    【DevOps】DevOps成功的八大炫酷工具
    【Network】Calico, Flannel, Weave and Docker Overlay Network 各种网络模型之间的区别
    【Network】UDP 大包怎么发? MTU怎么设置?
    【Network】高性能 UDP 应该怎么做?
    【Network】golang 容器项目 flannel/UDP相关资料
  • 原文地址:https://www.cnblogs.com/HJiang/p/7462230.html
Copyright © 2020-2023  润新知