博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发常用宏
阅读量:6887 次
发布时间:2019-06-27

本文共 3727 字,大约阅读时间需要 12 分钟。

大家都是知道使用宏不仅方便,而且可以提高开发效率。下面总结了iOS开发过程中的一些常用宏,会持续的往里面添加。

 
  1. //字符串是否为空 
  2. #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO ) 
  3. //数组是否为空 
  4. #define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0) 
  5. //字典是否为空 
  6. #define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0) 
  7. //是否是空对象 
  8. #define kObjectIsEmpty(_object) (_object == nil \ 
  9. || [_object isKindOfClass:[NSNull class]] \ 
  10. || ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \ 
  11. || ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0)) 
  12.   
  13. //获取屏幕宽度与高度 
  14. #define kScreenWidth \ 
  15. ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreenmainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.width) 
  16. #define kScreenHeight \ 
  17. ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreenmainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.height) 
  18. #define kScreenSize \ 
  19. ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? CGSizeMake([UIScreenmainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale,[UIScreenmainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale) : [UIScreen mainScreen].bounds.size
  20.   
  21. //一些缩写 
  22. #define kApplication        [UIApplication sharedApplication] 
  23. #define kKeyWindow          [UIApplication sharedApplication].keyWindow 
  24. #define kAppDelegate        [UIApplication sharedApplication].delegate 
  25. #define kUserDefaults       [NSUserDefaults standardUserDefaults] 
  26. #define kNotificationCenter [NSNotificationCenter defaultCenter] 
  27.   
  28. //APP版本号 
  29. #define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"
  30. //系统版本号 
  31. #define kSystemVersion [[UIDevice currentDevice] systemVersion] 
  32. //获取当前语言 
  33. #define kCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0]) 
  34. //判断是否为iPhone 
  35. #define kISiPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
  36. //判断是否为iPad 
  37. #define kISiPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
  38.   
  39. //获取沙盒Document路径 
  40. #define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] 
  41. //获取沙盒temp路径 
  42. #define kTempPath NSTemporaryDirectory() 
  43. //获取沙盒Cache路径 
  44. #define kCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] 
  45.   
  46. //判断是真机还是模拟器 
  47. #if TARGET_OS_IPHONE 
  48. //真机 
  49. #endif 
  50.   
  51. #if TARGET_IPHONE_SIMULATOR 
  52. //模拟器 
  53. #endif 
  54.   
  55. //开发的时候打印,但是发布的时候不打印的NSLog 
  56. #ifdef DEBUG 
  57. #define NSLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__]) 
  58. #else 
  59. #define NSLog(...) 
  60. #endif 
  61.   
  62. //颜色 
  63. #define kRGBColor(r, g, b)     [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0] 
  64. #define kRGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a] 
  65. #define kRandomColor  KRGBColor(arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0) 
  66.   
  67. #define kColorWithHex(rgbValue) \ 
  68. [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16)) / 255.0 \ 
  69. green:((float)((rgbValue & 0xFF00) >> 8)) / 255.0 \ 
  70. blue:((float)(rgbValue & 0xFF)) / 255.0 alpha:1.0] 
  71.   
  72. //弱引用/强引用 
  73. #define kWeakSelf(type)   __weak typeof(type) weak##type = type; 
  74. #define kStrongSelf(type) __strong typeof(type) type = weak##type; 
  75.   
  76. //由角度转换弧度 
  77. #define kDegreesToRadian(x)      (M_PI * (x) / 180.0) 
  78. //由弧度转换角度 
  79. #define kRadianToDegrees(radian) (radian * 180.0) / (M_PI) 
  80.   
  81. //获取一段时间间隔 
  82. #define kStartTime CFAbsoluteTime start = CFAbsoluteTimeGetCurrent(); 
  83. #define kEndTime   NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start) 

本文作者:佚名
来源:51CTO

转载地址:http://haabl.baihongyu.com/

你可能感兴趣的文章
利用Windows AD搭建KMS服务器
查看>>
java项目命名规则
查看>>
Understanding Spark Caching
查看>>
抓取服务器硬件信息脚本
查看>>
四种禁止下载软件的方法
查看>>
Domino 8.5.1 安装过程
查看>>
重构数据库设计
查看>>
【CentOS 7笔记32】,通配符、输入输出重定向#171116
查看>>
【CentOS 7笔记43】iptables nat表和iptables规则备份和恢复,#171130
查看>>
jQuery基础修炼圣典—DOM篇
查看>>
hyper-v关于avhd的问题
查看>>
2013年工作中遇到的20个问题:281-300
查看>>
shell脚本实现两个文件的逐行对比
查看>>
我的友情链接
查看>>
烂泥:haproxy与nginx、zabbix集成
查看>>
iptables kits
查看>>
MyEclipse 2014 系列 , MyEclipse 2013 系列 , MyEclipse 10 系列
查看>>
java使用log4j打出exception的栈信息
查看>>
SQL Server 自动生成订单号
查看>>
Linux网络配置要点
查看>>