zoukankan      html  css  js  c++  java
  • iOS 常用宏定义

    /*
        宏定义
        __VA_ARGS__ 表示多个参数部分
        ... 多个不确定的参数
        ## 连接符 左右相连
     */
    
    // 例子 1
    
    #define Zlog(...) NSLog(__VA_ARGS__)
    
    // 例子 2
    // 解释 必须添加@是因为autoreleasepool{}存在  __weak_self___
    #define weakify( x ) 
    _Pragma("clang diagnostic push") 
    _Pragma("clang diagnostic ignored "-Wshadow"") 
    autoreleasepool{} __weak __typeof__(x) __weak_##x##__ = x; 
    _Pragma("clang diagnostic pop")
    
    
    #define strongify( x ) 
    _Pragma("clang diagnostic push") 
    _Pragma("clang diagnostic ignored "-Wshadow"") 
    try{} @finally{} __typeof__(x) x = __weak_##x##__; 
    _Pragma("clang diagnostic pop")
    
    // 例子 3
    #undef  PERFORM_BLOCK_SAFELY
    #define PERFORM_BLOCK_SAFELY( b, ... ) if ( (b) ) { (b)(__VA_ARGS__); }
    
    // 例子 4
    #undef  dispatch_main_sync_safe
    #define dispatch_main_sync_safe(block) if ([NSThread isMainThread]) { block(); 
    } else { dispatch_sync(dispatch_get_main_queue(), block); }
    
    #undef  dispatch_main_async_safe
    #define dispatch_main_async_safe(block) if ([NSThread isMainThread]) { block(); 
    } else { dispatch_async(dispatch_get_main_queue(), block); }
    
    // 例子 5 单例实现
    #undef    singleton
    #define singleton( __class ) 
    property (nonatomic, readonly) __class * sharedInstance; 
    - (__class *)sharedInstance; 
    + (__class *)sharedInstance;
    
    //
    #undef    def_singleton
    #define def_singleton( __class ) 
    dynamic sharedInstance; 
    - (__class *)sharedInstance 
    { 
    return [__class sharedInstance]; 
    } 
    + (__class *)sharedInstance 
    { 
    static dispatch_once_t once; 
    static __strong id __singleton__ = nil; 
    dispatch_once( &once, ^{ __singleton__ = [[__class alloc] init]; } ); 
    return __singleton__; 
    }

  • 相关阅读:
    CentOS 6 安装 python and pip
    Qt for Windows:Qt 5.4.0 MinGW 静态编译版本制作 (转)
    Vim 扩展工具 vim-ide (转)
    centos yum 完全卸载依赖
    Linux修改Shell命令提示符及颜色
    tmux 命令
    网络分析shell脚本(实时流量+连接统计)
    ICMP:Internet控制报文协议
    读《分布式一致性原理》zookeeper运维
    同网段和不同网段设备通信原理详解
  • 原文地址:https://www.cnblogs.com/chaochaobuhuifei55/p/9100652.html
Copyright © 2011-2022 走看看