iOS 多快好省的宏定义

http://my.oschina.net/yongbin45/blog/150149

// 字符串:
#ifndef nilToEmpty
#define nilToEmpty(object) (object!=nil)?object:@""
#endif

#ifndef formatStringOfObject
#define formatStringOfObject(object) [NSString stringWithFormat:@"%@", object]
#endif

#ifndef nilToEmptyFormatStringOfObject
#define nilToEmptyFormatStringOfObject(object) formatStringOfObject(nilToEmpty(object))
#endif

// 图片:
#ifndef imagePath
#define imagePath(imageName) [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]
#endif

// 颜色
#define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define RGB(r, g, b) RGBA(r, g, b, 1.0f)
#define HEXCOLOR(c) [UIColor colorWithRed:((c>>16)&0xFF)/255.0f green:((c>>8)&0xFF)/255.0f blue:(c&0xFF)/255.0f alpha:1.0f];

// debug
#define debug(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])

// iOS 支持
#define SUPPORT_IPHONE_OS_VERSION(version) ( __IPHONE_OS_VERSION_MIN_REQUIRED <= version && __IPHONE_OS_VERSION_MAX_ALLOWED >= version)

// Application delegate
#define ApplicationDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])

// 主要单例
#define UserDefaults [NSUserDefaults standardUserDefaults]
#define NotificationCenter [NSNotificationCenter defaultCenter]
#define SharedApplication [UIApplication sharedApplication]

#define Bundle [NSBundle mainBundle]

#define MainScreen [UIScreen mainScreen]

// 网络指示
#define ShowNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = YES
#define HideNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = NO
#define NetworkActivityIndicatorVisible(x) [UIApplication sharedApplication].networkActivityIndicatorVisible = x

// 主要控件
#define NavBar self.navigationController.navigationBar
#define TabBar self.tabBarController.tabBar

// 大小尺寸
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height

#define NavBarHeight self.navigationController.navigationBar.bounds.size.height
#define TabBarHeight self.tabBarController.tabBar.bounds.size.height

#define TouchHeightDefault 44.0f
#define TouchHeightSmall 32.0f

#define ViewWidth(v) v.frame.size.width
#define ViewHeight(v) v.frame.size.height
#define ViewX(v) v.frame.origin.x
#define ViewY(v) v.frame.origin.y

#define SelfViewWidth self.view.bounds.size.width
#define SelfViewHeight self.view.bounds.size.height

#define RectX(rect) rect.origin.x
#define RectY(rect) rect.origin.y
#define RectWidth(rect) rect.size.width
#define RectHeight(rect) rect.size.height

#define RectSetWidth(rect, w) CGRectMake(RectX(rect), RectY(rect), w, RectHeight(rect))
#define RectSetHeight(rect, h) CGRectMake(RectX(rect), RectY(rect), RectWidth(rect), h)
#define RectSetX(rect, x) CGRectMake(x, RectY(rect), RectWidth(rect), RectHeight(rect))
#define RectSetY(rect, y) CGRectMake(RectX(rect), y, RectWidth(rect), RectHeight(rect))

#define RectSetSize(rect, w, h) CGRectMake(RectX(rect), RectY(rect), w, h)
#define RectSetOrigin(rect, x, y) CGRectMake(x, y, RectWidth(rect), RectHeight(rect))

// 内存管理
#if ! __has_feature(objc_arc)
#define SBAutorelease(__v) ([__v autorelease]);
#define SBReturnAutoreleased SBAutorelease

#define SBRetain(__v) ([__v retain]);
#define SBReturnRetained SBRetain

#define SBRelease(__v) ([__v release]);

#define SBDispatchQueueRelease(__v) (dispatch_release(__v));
#else
// -fobjc-arc
#define SBAutorelease(__v)
#define SBReturnAutoreleased(__v) (__v)

#define SBRetain(__v)
#define SBReturnRetained(__v) (__v)

#define SBRelease(__v)

#if TARGET_OS_IPHONE
// Compiling for iOS
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
// iOS 6.0 or later
#define SBDispatchQueueRelease(__v)
#else
// iOS 5.X or earlier
#define SBDispatchQueueRelease(__v) (dispatch_release(__v));
#endif
#else
// Compiling for Mac OS X
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
// Mac OS X 10.8 or later
#define SBDispatchQueueRelease(__v)
#else
// Mac OS X 10.7 or earlier
#define SBDispatchQueueRelease(__v) (dispatch_release(__v));
#endif
#endif
#endif

时间: 2024-08-06 03:41:19

iOS 多快好省的宏定义的相关文章

iOS 多快好省的宏

原文地址:http://my.oschina.net/yongbin45/blog/150149 // 字符串: #ifndef nilToEmpty #define nilToEmpty(object) (object!=nil)?object:@"" #endif #ifndef formatStringOfObject #define formatStringOfObject(object) [NSString stringWithFormat:@"%@",

iOS技巧,宏定义

1.NSlog  发布后不打印 #ifdef DEBUG// 如果有DEBUG这个宏就编译下面一句代码 #define DDLog(...) NSLog(__VA_ARGS__) #else // 如果没有DEBUG这个宏就编译下面一句代码 #define DDLog(...) #endif 2.三方库及其他整理 1.?AFNetworking   HTTP 网络请求库   2.?SDWebImage 图片缓存 3.?MBProgressHUD 加载中展示 4.MMDrawerControlle

iOS开发笔记--宏定义的黑魔法 - 宏菜鸟起飞手册

宏定义在C系开发中可以说占有举足轻重的作用.底层框架自不必说,为了编译优化和方便,以及跨平台能力,宏被大量使用,可以说底层开发离开define将寸步难行.而在更高层级进行开发时,我们会将更多的重心放在业务逻辑上,似乎对宏的使用和依赖并不多.但是使用宏定义的好处是不言自明的,在节省工作量的同时,代码可读性大大增加.如果想成为一个能写出漂亮优雅代码的开发者,宏定义绝对是必不可少的技能(虽然宏本身可能并不漂亮优雅XD).但是因为宏定义对于很多人来说,并不像业务逻辑那样是每天会接触的东西.即使是能偶尔使

iOS 常用的宏定义

============================================= --2015/12/9 -[ 更新 ] 1.判断是否为真机 or 模拟器[注:记得先判断是否为模拟器咯,否则会失败的哦] #if TARGET_IPHONE_SIMULATOR//模拟器 #define IS_IPHONE 0 #elif TARGET_OS_IPHONE//真机 #define IS_IPHONE 1 #endif 2.屏幕宽高 #pragma mark - 自定义宏 //考虑转屏的影响

iOS swift 常量 &amp;&amp; 宏定义

全局常量 在C和Objective-C语言源文件中定义的全局常量会自动地被Swift编译引进并做为Swift的全局常量. 预处理指令 Swift编译器不包含预处理器.取而代之的是,它充分利用了编译时属性,生成配置,和语言特性来完成相同的功能.因此,Swift没有引进预处理指令. 简单宏 在 C和Objective-C中,通常使用#define指令来定义一个简单的常数,在Swift,您可以使用全局常量来代替.例如:定义一个常数 的#define FADE_ANIMATION_DURATION 0.

ios中使用宏定义进行调试

第一种,在控制台上输出日志信息: #ifdef DEBUG #define DLog(format,...) NSLog((@"DLog %s - [Line %d] %s\n\n" format), __PRETTY_FUNCTION__,__LINE__, __FUNCTION__,##__VA_ARGS__) #else #define DLog(format,...) do {} while(0) #endif 让NSLog只在debug build的时候起作用.将这个功能添加

iOS开发常用宏定义

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #78492a } span.s1 { } span.s2 { font: 14.0px "PingFang SC" } // 加载本地xib #define kMainBundleLoadXib(xi

iOS单例 宏定义

#define singleton_interface(className) + (className *)shared##className; // @implementation #define singleton_implementation(className) static className *_instance; + (id)allocWithZone:(NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(

iOS常用define宏定义

1. 屏幕宽高及常用尺寸 #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height) #define NavigationBar_HEIGHT 44.0f#define TabBar_HEIGHT 49.0f#define StatusBar_HEIGHT 20.0f#define ToolsBar_HE