iOS 多快好省的宏(转)

原文地址:http://my.oschina.net/yongbin45/blog/150149

  1. // 字符串:
  2. #ifndef nilToEmpty
  3. #define nilToEmpty(object) (object!=nil)?object:@""
  4. #endif
  5.  
  6. #ifndef formatStringOfObject
  7. #define formatStringOfObject(object) [NSString stringWithFormat:@"%@", object]
  8. #endif
  9.  
  10. #ifndef nilToEmptyFormatStringOfObject
  11. #define nilToEmptyFormatStringOfObject(object) formatStringOfObject(nilToEmpty(object))
  12. #endif
  13.  
  14. // 图片:
  15. #ifndef imagePath
  16. #define imagePath(imageName) [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]
  17. #endif
  18.  
  19. // 颜色
  20. #define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
  21. #define RGB(r, g, b) RGBA(r, g, b, 1.0f)
  22. #define HEXCOLOR(c) [UIColor colorWithRed:((c>>16)&0xFF)/255.0f green:((c>>8)&0xFF)/255.0f blue:(c&0xFF)/255.0f alpha:1.0f];
  23.  
  24. // debug
  25. #define debug(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])
  26.  
  27. // iOS 支持
  28. #define SUPPORT_IPHONE_OS_VERSION(version) ( __IPHONE_OS_VERSION_MIN_REQUIRED <= version && __IPHONE_OS_VERSION_MAX_ALLOWED >= version)
  29.  
  30. // Application delegate
  31. #define ApplicationDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])
  32.  
  33. // 主要单例
  34. #define UserDefaults [NSUserDefaults standardUserDefaults]
  35. #define NotificationCenter [NSNotificationCenter defaultCenter]
  36. #define SharedApplication [UIApplication sharedApplication]
  37.  
  38. #define Bundle [NSBundle mainBundle]
  39.  
  40. #define MainScreen [UIScreen mainScreen]
  41.  
  42. // 网络指示
  43. #define ShowNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = YES
  44. #define HideNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = NO
  45. #define NetworkActivityIndicatorVisible(x) [UIApplication sharedApplication].networkActivityIndicatorVisible = x
  46.  
  47. // 主要控件
  48. #define NavBar self.navigationController.navigationBar
  49. #define TabBar self.tabBarController.tabBar
  50.  
  51. // 大小尺寸
  52. #define ScreenWidth [[UIScreen mainScreen] bounds].size.width
  53. #define ScreenHeight [[UIScreen mainScreen] bounds].size.height
  54.  
  55. #define NavBarHeight self.navigationController.navigationBar.bounds.size.height
  56. #define TabBarHeight self.tabBarController.tabBar.bounds.size.height
  57.  
  58. #define TouchHeightDefault 44.0f
  59. #define TouchHeightSmall 32.0f
  60.  
  61. #define ViewWidth(v) v.frame.size.width
  62. #define ViewHeight(v) v.frame.size.height
  63. #define ViewX(v) v.frame.origin.x
  64. #define ViewY(v) v.frame.origin.y
  65.  
  66. #define SelfViewWidth self.view.bounds.size.width
  67. #define SelfViewHeight self.view.bounds.size.height
  68.  
  69. #define RectX(rect) rect.origin.x
  70. #define RectY(rect) rect.origin.y
  71. #define RectWidth(rect) rect.size.width
  72. #define RectHeight(rect) rect.size.height
  73.  
  74. #define RectSetWidth(rect, w) CGRectMake(RectX(rect), RectY(rect), w, RectHeight(rect))
  75. #define RectSetHeight(rect, h) CGRectMake(RectX(rect), RectY(rect), RectWidth(rect), h)
  76. #define RectSetX(rect, x) CGRectMake(x, RectY(rect), RectWidth(rect), RectHeight(rect))
  77. #define RectSetY(rect, y) CGRectMake(RectX(rect), y, RectWidth(rect), RectHeight(rect))
  78.  
  79. #define RectSetSize(rect, w, h) CGRectMake(RectX(rect), RectY(rect), w, h)
  80. #define RectSetOrigin(rect, x, y) CGRectMake(x, y, RectWidth(rect), RectHeight(rect))
  81.  
  82. // 内存管理
  83. #if ! __has_feature(objc_arc)
  84. #define SBAutorelease(__v) ([__v autorelease]);
  85. #define SBReturnAutoreleased SBAutorelease
  86.  
  87. #define SBRetain(__v) ([__v retain]);
  88. #define SBReturnRetained SBRetain
  89.  
  90. #define SBRelease(__v) ([__v release]);
  91.  
  92. #define SBDispatchQueueRelease(__v) (dispatch_release(__v));
  93. #else
  94. // -fobjc-arc
  95. #define SBAutorelease(__v)
  96. #define SBReturnAutoreleased(__v) (__v)
  97.  
  98. #define SBRetain(__v)
  99. #define SBReturnRetained(__v) (__v)
  100.  
  101. #define SBRelease(__v)
  102.  
  103. #if TARGET_OS_IPHONE
  104. // Compiling for iOS
  105. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
  106. // iOS 6.0 or later
  107. #define SBDispatchQueueRelease(__v)
  108. #else
  109. // iOS 5.X or earlier
  110. #define SBDispatchQueueRelease(__v) (dispatch_release(__v));
  111. #endif
  112. #else
  113. // Compiling for Mac OS X
  114. #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
  115. // Mac OS X 10.8 or later
  116. #define SBDispatchQueueRelease(__v)
  117. #else
  118. // Mac OS X 10.7 or earlier
  119. #define SBDispatchQueueRelease(__v) (dispatch_release(__v));
  120. #endif
  121. #endif
  122. #endif

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

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

  1. //补充1
  2.  
  3. //----------------------图片----------------------------
  4.  
  5. //读取本地图片
  6. #define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]
  7.  
  8. //定义UIImage对象
  9. #define IMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]]
  10.  
  11. //定义UIImage对象
  12. #define ImageNamed(_pointer) [UIImage imageNamed:[UIUtil imageName:_pointer]]
  13.  
  14. //建议使用前两种宏定义,性能高于后者
  15. //----------------------图片----------------------------
  1. //补充2
  2.  
  3. //----------------------其他----------------------------
  4.  
  5. //方正黑体简体字体定义
  6. #define FONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F]
  7.  
  8. //定义一个API
  9. #define APIURL @"http://xxxxx/"
  10. //登陆API
  11. #define APILogin [APIURL stringByAppendingString:@"Login"]
  12.  
  13. //设置View的tag属性
  14. #define VIEWWITHTAG(_OBJECT, _TAG) [_OBJECT viewWithTag : _TAG]
  15. //程序的本地化,引用国际化的文件
  16. #define MyLocal(x, ...) NSLocalizedString(x, nil)
  17.  
  18. //G-C-D
  19. #define BACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
  20. #define MAIN(block) dispatch_async(dispatch_get_main_queue(),block)
  21.  
  22. //NSUserDefaults 实例化
  23. #define USER_DEFAULT [NSUserDefaults standardUserDefaults]
  24.  
  25. //由角度获取弧度 有弧度获取角度
  26. #define degreesToRadian(x) (M_PI * (x) / 180.0)
  27. #define radianToDegrees(radian) (radian*180.0)/(M_PI)
  28.  
  29. //单例化一个类
  30. #define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \
  31. \
  32. static classname *shared##classname = nil; \
  33. \
  34. + (classname *)shared##classname \
  35. { \
  36. @synchronized(self) \
  37. { \
  38. if (shared##classname == nil) \
  39. { \
  40. shared##classname = [[self alloc] init]; \
  41. } \
  42. } \
  43. \
  44. return shared##classname; \
  45. } \
  46. \
  47. + (id)allocWithZone:(NSZone *)zone \
  48. { \
  49. @synchronized(self) \
  50. { \
  51. if (shared##classname == nil) \
  52. { \
  53. shared##classname = [super allocWithZone:zone]; \
  54. return shared##classname; \
  55. } \
  56. } \
  57. \
  58. return nil; \
  59. } \
  60. \
  61. - (id)copyWithZone:(NSZone *)zone \
  62. { \
  63. return self; \
  64. }
  65.  
  66. #endif
  1. //补充3
  2.  
  3. //----------------------颜色类---------------------------
  4. // rgb颜色转换(16进制->10进制)
  5. #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]
  6.  
  7. //带有RGBA的颜色设置
  8. #define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
  9.  
  10. // 获取RGB颜色
  11. #define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
  12. #define RGB(r,g,b) RGBA(r,g,b,1.0f)
  13.  
  14. //背景色
  15. #define BACKGROUND_COLOR [UIColor colorWithRed:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0]
  16.  
  17. //清除背景色
  18. #define CLEARCOLOR [UIColor clearColor]
  19.  
  20. #pragma mark - color functions
  21. #define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
  22. #define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
  23.  
  24. //----------------------颜色类--------------------------
  1. //补充4
  2.  
  3. //----------------------系统----------------------------
  4.  
  5. //获取系统版本
  6. #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
  7. #define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]
  8.  
  9. //获取当前语言
  10. #define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
  11.  
  12. //判断是否 Retina屏、设备是否%fhone 5、是否是iPad
  13. #define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
  14. #define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
  15. #define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  16.  
  17. //判断是真机还是模拟器
  18. #if TARGET_OS_IPHONE
  19. //iPhone Device
  20. #endif
  21.  
  22. #if TARGET_IPHONE_SIMULATOR
  23. //iPhone Simulator
  24. #endif
  25.  
  26. //检查系统版本
  27. #define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
  28. #define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
  29. #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
  30. #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
  31. #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
  32.  
  33. //----------------------系统----------------------------

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. Python flask虚拟环境安装

    1.安装virtualenv 2.在当前路径下创建文件夹,启动虚拟环境 3.在使用虚拟环境前需激活,前面出现(env说明在虚拟环境中).虚拟环境中默认安装了pip,所以直接pip安装flask 4.在 ...

  2. 10.8 wtx模拟题题解

    填坑 orz w_x_c_q w_x_c_q的模拟赛(150pts,炸了) money 题目背景: 王小呆又陷入自己的梦里.(活在梦里...) 题目描述: 王小呆是一个有梦想的小菜鸡,那就是赚好多好多 ...

  3. 【模板】普通平衡树 Splay

    题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入xxx数 删除xxx数(若有多个相同的数,因只删除一个) 查询xxx数的排名(排名定义为比当前数小的数的个数 ...

  4. ssh 配置无密码登录

    下框中在管理机上运行: [root@master ~]# ssh-keygen -t rsa #它在/root/.ssh下生成id_rsa和id_rsa.pub两个文件 [root@master ~] ...

  5. zabbix监控nginx进程

    确认nginx有没有安装模块 然后在主站点下添加(二级站点) location /NginxStatus { stub_status on; access_log off; allow 127.0.0 ...

  6. Functions that return a function

    javascript学习中,经常会遇到闭包的问题,然后闭包的很多例子中又会遇到很多返回函数的闭包的例子程序.因为对闭包的理解还不够透彻,然后对于Functions rerurn a function产 ...

  7. react 中文文档案例六 (表单)

    class Reservation extends React.Component { constructor(props) { super(props); this.state = { isGoin ...

  8. Android模拟器手动设置经纬度坐标

    第一种方式可以在eclipse的DDMS中的Emulator control中设置,如下图 另一种是在cmd中输入telnet localhost 5554(注:5554是模拟器在本机的端口,有可能不 ...

  9. Python十大应用领域与就业方向

    参考链接:https://baijiahao.baidu.com/s?id=1604847283884842928&wfr=spider&for=pc 正文: 近些年,编程语言Pyth ...

  10. Codeforces Round #334(div.2)(新增不用二分代码) B

    B. More Cowbell time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...