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

iOS开发中那些高效常用的宏(转)

原文地址:http://blog.csdn.net/duxinfeng2010/article/details/9067947

//补充1

//----------------------图片----------------------------  

//读取本地图片
#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:[UIUtil imageName:_pointer]] //建议使用前两种宏定义,性能高于后者
//----------------------图片----------------------------
//补充2

//----------------------其他----------------------------  

//方正黑体简体字体定义
#define FONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F] //定义一个API
#define APIURL @"http://xxxxx/"
//登陆API
#define APILogin [APIURL stringByAppendingString:@"Login"] //设置View的tag属性
#define VIEWWITHTAG(_OBJECT, _TAG) [_OBJECT viewWithTag : _TAG]
//程序的本地化,引用国际化的文件
#define MyLocal(x, ...) NSLocalizedString(x, nil) //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) //NSUserDefaults 实例化
#define USER_DEFAULT [NSUserDefaults standardUserDefaults] //由角度获取弧度 有弧度获取角度
#define degreesToRadian(x) (M_PI * (x) / 180.0)
#define radianToDegrees(radian) (radian*180.0)/(M_PI) //单例化一个类
#define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \
\
static classname *shared##classname = nil; \
\
+ (classname *)shared##classname \
{ \
@synchronized(self) \
{ \
if (shared##classname == nil) \
{ \
shared##classname = [[self alloc] init]; \
} \
} \
\
return shared##classname; \
} \
\
+ (id)allocWithZone:(NSZone *)zone \
{ \
@synchronized(self) \
{ \
if (shared##classname == nil) \
{ \
shared##classname = [super allocWithZone:zone]; \
return shared##classname; \
} \
} \
\
return nil; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return self; \
} #endif
//补充3

//----------------------颜色类---------------------------
// rgb颜色转换(16进制->10进制)
#define UIColorFromRGB(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] //带有RGBA的颜色设置
#define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A] // 获取RGB颜色
#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 BACKGROUND_COLOR [UIColor colorWithRed:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0] //清除背景色
#define CLEARCOLOR [UIColor clearColor] #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)] //----------------------颜色类--------------------------
//补充4

//----------------------系统----------------------------  

//获取系统版本
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#define CurrentSystemVersion [[UIDevice currentDevice] systemVersion] //获取当前语言
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0]) //判断是否 Retina屏、设备是否%fhone 5、是否是iPad
#define isRetina ([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 isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //判断是真机还是模拟器
#if TARGET_OS_IPHONE
//iPhone Device
#endif #if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif //检查系统版本
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending) //----------------------系统----------------------------

iOS重用宏定义的更多相关文章

  1. iOS 使用宏定义函数和代码块

    iOS使用宏定义函数和代码块 今天在开发过程中碰到一个问题:就是父类中要向外发送通知,然后子类中或者其他类中来接收它.当然一般是把它写到类方法中去,但是有个问题,就是如果调用的类不是它的子类,就不能直 ...

  2. iOS常用宏 定义

    总结了iOS开发过程中的一些常用宏,以后会陆陆续续添加进来. 字符串是否为空 1   #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull c ...

  3. iOS常用宏定义--实用

    在这里给大家分享一些常用的宏定义,喜欢的小伙伴可以直接在项目中使用(持续更新)!为了大家使用方便,请点击GitHub - 宏定义头文件下载 ! 1.获取屏幕宽度与高度 #define SCREEN_W ...

  4. iOS常用宏定义大全

    宏定义与常量的区别 宏:只是在预处理器里进行文本替换,不做任何类型检查,宏能定义代码,const不能,多个宏编译时间相对较长,影响开发效率,调试过慢,const只会编译一次,缩短编译时间. 所以在使用 ...

  5. iOS之宏定义#define

    最基本的宏定义用法 #define aaa bbb 表示用aaa替换bbb的内容. 宏作用范围 宏的作用范围是在当前文件内, 如果需要作用于其他类(如在类b调用类a已定义宏),那么需要在类b引入类a的 ...

  6. iOS常用宏定义

    转发:https://www.douban.com/note/486674206/ #ifndef MacroDefinition_h#define MacroDefinition_h //----- ...

  7. iOS define 宏定义 和 const定义常量区别

    const   const 是c++中的修饰符.  c++中常用来定义常量,修饰左值. #define 宏定义语句, 在预处理阶段直接做文本替换,不做类型检查. 它们之间的最大区别: 1.  对于co ...

  8. (转)iOS 常用宏定义

    #ifndef MacroDefinition_h #define MacroDefinition_h   //-------------------获取设备大小------------------- ...

  9. iOS - 常用宏定义和PCH文件知识点整理

    (一)PCH文件操作步骤演示: 第一步:图文所示: 第二步:图文所示: (二)常用宏定义整理: (1)常用Log日志宏(输出日志详细可定位某个类.某个函数.某一行) //=============== ...

随机推荐

  1. nginx 简单教程

    使用 nginx 的使用比较简单,就是几条命令. 常用到的命令如下: nginx -s stop 快速关闭Nginx,可能不保存相关信息,并迅速终止web服务. nginx -s quit 平稳关闭N ...

  2. selenium+Node.js在windows下的配置和安装

    转载:http://www.jianshu.com/p/5e64bb70abb8

  3. DB2存储过程标准

    CREATE OR REPLACE PROCEDURE "FCT"."PROC_FCT_DSB_SERIES"(IN ACCOUNTING_DATE DATE) ...

  4. NSFileManager在初始化文件的时候一不留神就进入陷阱

    今天调试一个程序,内容是在手机一个本地路径生成一个缓存文件,在生成本地路径的时候犯了一个错误,本着求原因的精神调试了2个小时,终于找到原因了 刚开始断点调试的时候,执行到第13行,这里死活不给写入数据 ...

  5. springboot整合dubbo注解方式(三)

    笔者用自己代码测试时候发现这个版本信息,可以没有,也可以制定一个就可以了,没有太大的问题,只是需要注意引入的依赖问题,两个jar与一个jar的问题, 然后就是接口公共依赖问题,案例是把我找的几个案例进 ...

  6. linux中tomcat内存溢出

    刚开始测试服务器与线上后台都不能上传10分钟以上的视频,后来只要是视频就不能上传,进入服务器查日志得到如下错误: Caused by: java.lang.OutOfMemoryError: Java ...

  7. [JavaScript]Prototype继承

    JavaScript相对于其他的编程语言是比较简单的,只要吃透了Prototype和Closure(闭包),基本上就可以说精通JavaScript了. JavaScript里如何实现向Java语言的O ...

  8. HDU2665(可持久化线段树板子)

    1.题意有坑,实际要求第k小. 2.没学过动态开点也没学过主席树,看一下博主思路然后妄想自己实现的后果就是拿命去调bug. const int maxn = 1e5 + 5; int test, n, ...

  9. d题

    #include<iostream>#include<algorithm>using namespace std;int a[200005];int b[200005];int ...

  10. app内部H5测试点总结

    1.业务逻辑 除基本功能测试外,需要关注的一些测试点: a.登录 a.1 H5页面嵌入到客户端使用,若客户端已经登录,进入H5页面应该是登录状态 a.2 H5页面嵌入到客户端内使用,若客户端未登录,如 ...