ios开发常用的宏
转自 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)]; |