【Objective-C】常用的宏定義

/// App版本號(hào)
#define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
/// App內(nèi)建版本號(hào)
#define kAppBuildVersion  [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]
/// 系統(tǒng)版本號(hào)
#define kSystemVersion [[UIDevice currentDevice] systemVersion]

/// 不同系統(tǒng)版本號(hào)
#define IOS8 ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0 && [[UIDevice currentDevice].systemVersion doubleValue] < 9.0)
#define IOS9 ([[UIDevice currentDevice].systemVersion doubleValue] >= 9.0 && [[UIDevice currentDevice].systemVersion doubleValue] < 10.0)
#define IOS10 ([[UIDevice currentDevice].systemVersion doubleValue] >= 10.0 && [[UIDevice currentDevice].systemVersion doubleValue] < 11.0)
#define IOS11 ([[UIDevice currentDevice].systemVersion doubleValue] >= 11.0 && [[UIDevice currentDevice].systemVersion doubleValue] < 12.0)
#define IOS12 ([[UIDevice currentDevice].systemVersion doubleValue] >= 12.0 && [[UIDevice currentDevice].systemVersion doubleValue] < 13.0)
#define IOS13 ([[UIDevice currentDevice].systemVersion doubleValue] >= 13.0 && [[UIDevice currentDevice].systemVersion doubleValue] < 14.0)
#define IOS14 ([[UIDevice currentDevice].systemVersion doubleValue] >= 14.0 && [[UIDevice currentDevice].systemVersion doubleValue] < 15.0)

#define IOS8_LATER @available(iOS 8.0, *)
#define IOS9_LATER @available(iOS 9.0, *)
#define IOS10_LATER @available(iOS 10.0, *)
#define IOS11_LATER @available(iOS 11.0, *)
#define IOS12_LATER @available(iOS 12.0, *)
#define IOS13_LATER @available(iOS 13.0, *)
#define IOS14_LATER @available(iOS 14.0, *)


/// Document文件路徑
#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
/// Library文件路徑
#define kLibraryPath [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject]
/// Cache文件路徑
#define kCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
/// Temporary文件路徑
#define kTempPath NSTemporaryDirectory()


/// Application
#define kApplication        [UIApplication sharedApplication]
/// Appdelegate
#define kAppDelegate        [UIApplication sharedApplication].delegate
/// UserDefaults
#define kUserDefaults      [NSUserDefaults standardUserDefaults]
/// NotificationCenter
#define kNotificationCenter [NSNotificationCenter defaultCenter]


/// 判斷字符串是否為空
#define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
/// 判斷數(shù)組是否為空
#define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
/// 判斷字典是否為空
#define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
/// 判斷是否為空對象
#define kObjectIsEmpty(_object) (_object == nil \
|| [_object isKindOfClass:[NSNull class]] \
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))


/// 屏幕寬度
#define kScreenW \
([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.width)
/// 屏幕高度
#define kScreenH \
([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.height)
/// 屏幕尺寸
#define kScreenS \
([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? CGSizeMake([UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale,[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale) : [UIScreen mainScreen].bounds.size)


/// 屏幕適配(以屏幕寬度為375的設(shè)計(jì)圖為準(zhǔn))
#define Scale(x) ((kScreenW>=375)?x:kScreenW*(x)/375.0)


/// GCD - 一次性執(zhí)行
#define DISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);
/// GCD - 在Main線程上運(yùn)行
#define DISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);
/// GCD - 開啟異步線程
#define DISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);
/// GCD - 定時(shí)器
#define DISPATCH_AFTER_BLOCK(duartion,onceBlock) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duartion * NSEC_PER_SEC)), dispatch_get_main_queue(),onceBlock);


/// 拼接字符串
#define NSStringFormat(format,...) [NSString stringWithFormat:format,##__VA_ARGS__]
/// 加載本地圖片
#define ImageNamed(name) [UIImage imageNamed:[UIUtil imageName:name]]


/// RGB顏色
#define RGB(r, g, b)          [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
/// RGBA顏色
#define RGBA(r, g, b, a)      [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]
/// 隨機(jī)顏色
#define RandomColor                KRGBColor(arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0)
/// HEX顏色
#define HexColor  (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]


/// 系統(tǒng)默認(rèn)字體
#define SystemFont(x) [UIFont systemFontOfSize:x]
/// 系統(tǒng)粗體
#define BoldFont(x) [UIFont boldSystemFontOfSize:x]
/// 系統(tǒng)中體
#define MediumFont(x) [UIFont fontWithName:@"PingFangSC-Medium" size:x]


/// 角度 轉(zhuǎn) 弧度
#define DegreesToRadian(x)      (M_PI * (x) / 180.0)
/// 弧度 轉(zhuǎn) 角度
#define RadianToDegrees(radian) (radian * 180.0) / (M_PI)


/// 時(shí)間間隔:開始時(shí)間
#define StartTime CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
/// 時(shí)間間隔:結(jié)束時(shí)間
#define EndTime  NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)


/// 控制臺(tái)打印
#ifdef DEBUG
#define NSLog(...) NSLog(@"%s 第%d行 : %@\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define NSLog(...)
#endif

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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