OC共通マクロ定義

4983 ワード

  • 試験出力Log
  • #pragma mark - DEBUG
    #ifdef DEBUG
    //  Log
    #define DLog(format, ...) NSLog(@"Line[%d] %s " format, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__)
    #else
    //  Log
    #define DLog(format, ...)
    #endif
    //  
    #define LogClassName DLog(@"")
    
  • スクリーン関連
  • #pragma mark -  
    #define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
    #define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
    #define iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
    #define iPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
    #define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    #define isPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    
    #define kScreenHeight [[UIScreen mainScreen] bounds].size.height
    #define kScreenWidth  [[UIScreen mainScreen] bounds].size.width
    #define kStatusBarHeight 20
    #define kNavigationBarHeight 44
    #define kNavigationBarIcon 20
    #define kTabBarHeight 49
    #define kTabBarIcon 30
    
  • システム関連
  • #pragma mark -  
    //  
    #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)
    
    //   
    #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue] 
    #define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]
    
    //  
    #define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
    
    //  
    #if TARGET_OS_IPHONE 
        //iPhone Device 
    #endif 
    #if TARGET_IPHONE_SIMULATOR 
        //iPhone Simulator 
    #endif
    
    // ARC ARC 
    #if __has_feature(objc_arc) 
        //compiling with ARC 
    #else 
        // compiling without ARC 
    #endif
    
  • 弱引用、強引用
  • を定義する.
    #define WeakSelf(weakSelf)  __weak __typeof(&*self)weakSelf = self
    
    #define ESWeak(var, weakVar) __weak __typeof(&*var) weakVar = var
    #define ESStrong_DoNotCheckNil(weakVar, _var) __typeof(&*weakVar) _var = weakVar
    #define ESStrong(weakVar, _var) ESStrong_DoNotCheckNil(weakVar, _var); if (!_var) return;
    
    #define ESWeak_(var) ESWeak(var, weak_##var);
    #define ESStrong_(var) ESStrong(weak_##var, _##var);
    
    /** defines a weak `self` named `weakSelf` */
    #define ESWeakSelf      ESWeak(self, weakSelf);
    /** defines a strong `self` named `strongSelf` from `weakSelf` */
    #define ESStrongSelf    ESStrong(weakSelf, strongSelf);
    
  • 定義警告マクロ
  • #define SuppressPerformSelectorLeakWarning(Stuff) \
    do { \
    _Pragma("clang diagnostic push") \
    _Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
    Stuff; \
    _Pragma("clang diagnostic pop") \
    } while (0)
    
  • カラーマクロ
  • //  
    #define RANDOM_COLOR [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1]
    
    // 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] 
    
    #define RGBACOLOR(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
    #define RGBCOLOR(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1.0f] 
    //#define RGBCOLOR(r, g, b) RGBACOLOR(r, g, b, 1.0f) 
    
  • 他のマクロ
  • //G-C-D 
    #define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block) 
    #define MAIN(block) dispatch_async(dispatch_get_main_queue(), block)
    
    //    
    #define degreesToRadian(x) (M_PI * (x) / 180.0) 
    #define radianToDegrees(radian) (radian*180.0)/(M_PI)
    
    //  :http://www.jianshu.com/p/7c3ee5e67d03
    //  block 
    #define BLOCK_EXEC(block, ...) if (block) { block(__VA_ARGS__); }
    //  
    if (completionBlock) {   
      completionBlock(arg1, arg2); 
    }    
    //  
    BLOCK_EXEC(completionBlock, arg1, arg2);