學(xué)習(xí)筆記-iOS常用宏定義

  • 開(kāi)發(fā)的時(shí)候打印,但是發(fā)布的時(shí)候不打印
#ifdef DEBUG
  #define NSLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define NSLog(...)
#endif
  • 強(qiáng)/弱引用
 #define strongSelf(type) __strong typeof(type) type = weak##type;
 #define weakSelf(type)  __weak typeof(type) weak##type = type;
  • RGB顏色 16進(jìn)制->10進(jìn)制
#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]
// 使用示例
    self.view.backgroundColor = UIColorFromRGB(0xFFB6C1);
  • RGBA顏色
#define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
  • 獲取屏幕寬度、高度
#define ScreenWidth  ([UIScreen mainScreen].applicationFrame.size.width)
#define ScreenHeight ([UIScreen mainScreen].applicationFrame.size.height)
  • 獲取Document路徑
#define DocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
  • 獲取Temp路徑
#define TempPath NSTemporaryDirectory()
  • 獲取Cache路徑
#define CachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
  • 字符串是否為空
#define StringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
  • 數(shù)組是否為空
#define ArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
  • 字典是否為空
#define DictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
  • 對(duì)象是否為空
#define ObjectIsEmpty(_object) (_object == nil \
  || [_object isKindOfClass:[NSNull class]] \
  || ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
  || ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))
  • 系統(tǒng)版本檢測(cè)
 #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)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容