iOS 常用宏(持續更新)

是否為空

//字符串是否為空
#define strIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length]<1 ? YES : NO )

//數組是否為空
#define arrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)

//字典是否為空
#define dicIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)

// 檢查空對象
#define OBJECT_IS_EMPTY(_object) (_object == nil || [_object isKindOfClass:[NSNull class]] || ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) || ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))

圖片

//讀取本地圖片
#define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[NSBundle mainBundle]pathForResource:(file) ofType:(ext)]
//定義UIImage對象
#define IMAGE(A) [UIImage imageWithContentsOfFile:[NSBundle mainBundle] pathForResource:(A) ofType:nil]
//定義UIImage對象
#define ImageNamed(_pointer) [UIImage imageNamed:(_pointer)]
//建議使用前兩種宏定義,性能高于后者//

屏幕

//獲取屏幕寬度與高度
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height

// View 坐標(x,y)和寬高(width,height)
#define X(v)                    (v).frame.origin.x
#define Y(v)                    (v).frame.origin.y

#define WIDTH(v)                (v).frame.size.width
#define HEIGHT(v)               (v).frame.size.height

#define MinX(v)                 CGRectGetMinX((v).frame)
#define MinY(v)                 CGRectGetMinY((v).frame)

#define MidX(v)                 CGRectGetMidX((v).frame)
#define MidY(v)                 CGRectGetMidY((v).frame)

#define MaxX(v)                 CGRectGetMaxX((v).frame)
#define MaxY(v)                 CGRectGetMaxY((v).frame)

//RGB
#define kRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define kRGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]
#define kRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]

//! 參數格式為:0xFFFFFF  
#define kColorWithRGB(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 SINGLE_LINE_WIDTH (1 / [UIScreen mainScreen].scale)

系統

//當前手機系統版本
#define SystemVersion          ([[UIDevice currentDevice] systemVersion])
//是否iPad
#define isPad                   (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
//App版本號
#define appMPVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]

//判斷是真機還是模擬器
#if TARGET_OS_IPHONE 
    //iPhone Device 
#endif #if TARGET_IPHONE_SIMULATOR 
    //iPhone Simulator 
#endif

//判斷當前版本適配(新增)
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@interface ZXCapture ()<CALayerDelegate>
#else
@interface ZXCapture ()
#endif

提示

//自定義高效率的 NSLog
//項目開發中,我們會在許多地方加上Log,但是發布的時候又不想用這些Log,我們也不可能一個一個的刪除,所以自定義Log是必然的!
#ifdef DEBUG
#define DLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define DLog(...)
#endif

#define kALERT(msg) [[[UIAlertView alloc] initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"確認" otherButtonTitles:nil] show]

引用

//弱引用/強引用
#define kWeakSelf(type) __weak typeof(type) weak##type = type;
#define kStrongSelf(type) __strong typeof(type) type = weak##type;

沙盒

//沙盒目錄文件
//獲取temp
#define kPathTemp NSTemporaryDirectory()

//獲取沙盒 Document
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]

//獲取沙盒 Cache
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

GCD

//GCD - 一次性執行
#define kDISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);

//GCD - 在Main線程上運行
#define kDISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);

//GCD - 開啟異步線程
#define kDISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);

//GCD - 延遲0.5
#define kDISPATCH_AFTER(mainQueueBlock) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{mainQueueBlock});
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容