1. 程式人生 > >ios開發常用的宏

ios開發常用的宏

sso dex osi comm ignore scac va_arg pragma weakself

轉自 http://www.cocoachina.com/bbs/read.php?tid=1719540

#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 這個沒用過, 你們也可以試試 #ifdef DEBUG # define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); #else # define DLog(...) #endif // 這個本人常用的宏,只感覺時間對不上,其他很穩定 #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版本下打印輸出函數替換,以及標識 @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 打印 //手機型號 #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 //判斷是否真機 #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) // 不堵塞線程並在主線程的延遲執行 timer:延遲時間,單位:秒;與主線程同步 #define GCDMainDelay(timer,block) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, INT64_C(timer) * NSEC_PER_SEC), dispatch_get_main_queue(), block) // 將code調到主線程執行,與主線程同步 #define GCDMain(block) dispatch_async(dispatch_get_main_queue(), block) #define USER_DEFAULT [NSUserDefaults standardUserDefaults] // 獲取 NSUserDefaults 對象 #define ImageNamed(_pointer) [UIImage imageNamed:[UIUtil imageName:_pointer]] // 傳入圖片名稱,獲取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; /** 強引用*/ #define kStrongSelf(type) __strong typeof(type) type = weak##type; /** 由角度轉換弧度*/ #define kDegreesToRadian(x) (M_PI * (x) / 180.0) /** 由弧度轉換角度*/ #define kRadianToDegrees(radian) (radian * 180.0) / (M_PI) /** 獲取一段時間間隔*/ #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 ) /** 數組是否為空*/ #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)) /** 判斷是否為iPhone*/ #define kISiPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) /** 判斷是否為iPad*/ #define kISiPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) /** 判斷是真機還是模擬器*/ #if TARGET_OS_IPHONE //真機 #endif #if TARGET_IPHONE_SIMULATOR //模擬器 #endif #pragma mark - 縮寫 #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 重啟會清空)*/ #define kTempPath NSTemporaryDirectory() /** 獲取沙盒 Cache 路徑*/ #define kCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] /** 獲取程序包中程序路徑*/ #define kResource(f, t) [[NSBundle mainBundle] pathForResource:(f) ofType:(t)];

ios開發常用的宏