iOS開(kāi)發(fā)中常用的宏

以下為iOS開(kāi)發(fā)中常用宏:


#define NavigationBar_HEIGHT 44
  
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define SAFE_RELEASE(x) [x release];x=nil
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#define CurrentSystemVersion ([[UIDevice currentDevice] systemVersion])  
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0]) 
  
#define BACKGROUND_COLOR [UIColor colorWithRed:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0]
  
  
 //----------------------------------------------------------------------------------------------------------------------------------LOG 打印 
//use dlog to print while in debug model     這個(gè)沒(méi)用過(guò), 你們也可以試試
#ifdef DEBUG
#   define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#   define DLog(...)
#endif
 
// 這個(gè)本人常用的宏,只感覺(jué)時(shí)間對(duì)不上,其他很穩(wěn)定
#ifdef DEBUG
#define NSLog(FORMAT, ...) fprintf(__stderrp,"%s %s:%d\t%s\t%s\n",__TIME__,[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, __FUNCTION__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define NSLog(...)
#endif
 
 debug或者release版本下打印輸出函數(shù)替換,以及標(biāo)識(shí)
@param format nil
@param ... nil
@return debug下TL_ISDEBUG為YES,release下TL_ISDEBUG為NO
*/
#ifdef DEBUG
#define TL_CLog(format, ...)  NSLog(format, ## __VA_ARGS__)
#    define TL_ISDEBUG    1
#else
#define TL_CLog(format,...)
#    define TL_ISDEBUG    0
#endif 
 //----------------------------------------------------------------------------------------------------------------------------------LOG 打印
  
//手機(jī)型號(hào)
#define iPhone4 ([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)
  
  
#if TARGET_OS_IPHONE
//iPhone Device
//判斷是否真機(jī)
#endif
  
#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
//判斷是否模擬器
#endif
  
  
//ARC
#if __has_feature(objc_arc)
    //compiling with ARC
#else
    // compiling without ARC
#endif
  
  
//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)
 
// 不堵塞線程并在主線程的延遲執(zhí)行 timer:延遲時(shí)間,單位:秒;與主線程同步
#define GCDMainDelay(timer,block) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, INT64_C(timer) * NSEC_PER_SEC), dispatch_get_main_queue(), block)
 
// 將code調(diào)到主線程執(zhí)行,與主線程同步
#define GCDMain(block) dispatch_async(dispatch_get_main_queue(), block) 
 
  
#define USER_DEFAULT [NSUserDefaults standardUserDefaults] // 獲取 NSUserDefaults  對(duì)象
#define ImageNamed(_pointer) [UIImage imageNamed:[UIUtil imageName:_pointer]] // 傳入圖片名稱(chēng),獲取UIImage
  
#pragma mark - common functions 
#define RELEASE_SAFELY(__POINTER) { [__POINTER release]; __POINTER = nil; }
  
  
#pragma mark - degrees/radian functions 
#define degreesToRadian(x) (M_PI * (x) / 180.0)
#define radianToDegrees(radian) (radian*180.0)/(M_PI)
  
#pragma mark - color functions 
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
#define ITTDEBUG
#define ITTLOGLEVEL_INFO     10
#define ITTLOGLEVEL_WARNING  3
#define ITTLOGLEVEL_ERROR    1
  
#ifndef ITTMAXLOGLEVEL
  
#ifdef DEBUG
    #define ITTMAXLOGLEVEL ITTLOGLEVEL_INFO
#else
    #define ITTMAXLOGLEVEL ITTLOGLEVEL_ERROR
#endif
  
#endif
  
// The general purpose logger. This ignores logging levels.
#ifdef ITTDEBUG
  #define ITTDPRINT(xx, ...)  NSLog(@"%s(%d): " xx, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
  #define ITTDPRINT(xx, ...)  ((void)0)
#endif
  
// Prints the current method's name.
#define ITTDPRINTMETHODNAME() ITTDPRINT(@"%s", __PRETTY_FUNCTION__)
  
// Log-level based logging macros.
#if ITTLOGLEVEL_ERROR <= ITTMAXLOGLEVEL
  #define ITTDERROR(xx, ...)  ITTDPRINT(xx, ##__VA_ARGS__)
#else
  #define ITTDERROR(xx, ...)  ((void)0)
#endif
  
#if ITTLOGLEVEL_WARNING <= ITTMAXLOGLEVEL
  #define ITTDWARNING(xx, ...)  ITTDPRINT(xx, ##__VA_ARGS__)
#else
  #define ITTDWARNING(xx, ...)  ((void)0)
#endif
  
#if ITTLOGLEVEL_INFO <= ITTMAXLOGLEVEL
  #define ITTDINFO(xx, ...)  ITTDPRINT(xx, ##__VA_ARGS__)
#else
  #define ITTDINFO(xx, ...)  ((void)0)
#endif
  
#ifdef ITTDEBUG
  #define ITTDCONDITIONLOG(condition, xx, ...) { if ((condition)) { \
                                                  ITTDPRINT(xx, ##__VA_ARGS__); \
                                                } \
                                              } ((void)0)
#else
  #define ITTDCONDITIONLOG(condition, xx, ...) ((void)0)
#endif
  
#define ITTAssert(condition, ...)                                       \
do {                                                                      \
    if (!(condition)) {                                                     \
        [[NSAssertionHandler currentHandler]                                  \
            handleFailureInFunction:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
                                file:[NSString stringWithUTF8String:__FILE__]  \
                            lineNumber:__LINE__                                  \
                            description:__VA_ARGS__];                             \
    }                                                                       \
} while(0)
  
  
  
#define _po(o) DLOG(@"%@", (o))
#define _pn(o) DLOG(@"%d", (o))
#define _pf(o) DLOG(@"%f", (o))
#define _ps(o) DLOG(@"CGSize: {%.0f, %.0f}", (o).width, (o).height)
#define _pr(o) DLOG(@"NSRect: {{%.0f, %.0f}, {%.0f, %.0f}}", (o).origin.x, (o).origin.x, (o).size.width, (o).size.height)
  
#define DOBJ(obj)  DLOG(@"%s: %@", #obj, [(obj) description])
  
#define MARK    NSLog(@"\nMARK: %s, %d", __PRETTY_FUNCTION__, __LINE__)
 
#pragma mark - Base
/** 弱引用*/
#define kWeakSelf(type)   __weak typeof(type) weak##type = type;
/** 強(qiáng)引用*/
 
#define kStrongSelf(type) __strong typeof(type) type = weak##type;
/** 由角度轉(zhuǎn)換弧度*/
#define kDegreesToRadian(x)      (M_PI * (x) / 180.0)
/** 由弧度轉(zhuǎn)換角度*/
#define kRadianToDegrees(radian) (radian * 180.0) / (M_PI)
 
/** 獲取一段時(shí)間間隔*/
#define kStartTime  CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
#define kEndTime    NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start) 
 
#pragma mark - Check
/** 字符串是否為空*/
#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)
/** 是否是空對(duì)象*/
#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))
/** 判斷是否為iPhone*/
#define kISiPhone   (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
/** 判斷是否為iPad*/
#define kISiPad     (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
 
/** 判斷是真機(jī)還是模擬器*/
#if TARGET_OS_IPHONE
//真機(jī)
#endif
#if TARGET_IPHONE_SIMULATOR
//模擬器
#endif 
 
#pragma mark - 縮寫(xiě)
#define kApplication        [UIApplication sharedApplication]
#define kKeyWindow          [UIApplication sharedApplication].keyWindow
#define kAppDelegate        ((AppDelegate*)[UIApplication sharedApplication].delegate)
#define kUserDefaults       [NSUserDefaults standardUserDefaults]
#define kNotificationCenter [NSNotificationCenter defaultCenter] 
 
/** 獲取沙盒 Document 路徑*/
#define kDocumentPath       [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
/** 獲取沙盒 temp 路徑(注:iPhone 重啟會(huì)清空)*/
#define kTempPath           NSTemporaryDirectory()
/** 獲取沙盒 Cache 路徑*/
#define kCachePath          [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
/** 獲取程序包中程序路徑*/
#define kResource(f, t)     [[NSBundle mainBundle] pathForResource:(f) ofType:(t)];

///------ 應(yīng)用程序版本號(hào)version ------
#define kAppVersion ([[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"])

///當(dāng)前日期字符串
#define DATE_STRING \
({NSDateFormatter *fmt = [[NSDateFormatter alloc] init];\
[fmt setDateFormat:@"YYYY-MM-dd hh:mm:ss"];\
[fmt stringFromDate:[NSDate date]];})

///------ other ------
#define kAppDelegate ([[UIApplication sharedApplication] delegate])
#define kAppWindow (kAppDelegate.window)
#define kAppRootViewController (kAppWindow.rootViewController)

#define UserDefaults ([NSUserDefaults standardUserDefaults])
#define NotificationCenter ([NSNotificationCenter defaultCenter])


引用:ios開(kāi)發(fā)常用的宏,大家一起來(lái)收集
參考:ios開(kāi)發(fā)常用的宏,大家一起來(lái)收集~

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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