iOS開發常用宏

  • 尺寸相關
//狀態欄高度
#define kStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
  • 顏色相關
//十六進制
#define UIColorFromHexadecimalRGB(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 UIColorFromHexadecimalRGBA(rgbValue,a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:a]

//十進制
#define UIColorFromRGB(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define UIColorFromRGBA(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
  • 開發環境宏
#if DEBUG
    //只在測試環境下執行
#endif
#if RELEASE
    //只在線上環境下執行
#endif
  • 調試宏 (輸出方法名 行號 內容)
#define NSLog(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
  • 線程
//主線程調用方法
#define CallForMainQueue(__function__)\
if([NSThread isMainThread]){\
__function__;\
}\
else\
{\
dispatch_sync(dispatch_get_main_queue(),^{\
__function__;\
});\
}\

//全局隊列異步調用方法
#define CallForAsyncQueue(__function__)(\
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{\
__function__;\
})\
)
  • 編譯版本號
#define kShortVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
  • 強弱應用
#define kWeak(obj)   __weak typeof(obj) weak##obj = obj
#define kStrong(obj) __strong typeof(obj) strong##obj = weak##obj;
  • 沙盒Document目錄
#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
``
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章