iOS开发常用宏

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

[[185940]]

大家都是知道使用宏不仅方便,而且可以提高开发效率。下面总结了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) 

 

责任编辑:庞桂玉 来源: iOS大全
相关推荐

2018-01-16 15:06:36

iPhone XiOS

2014-04-09 10:51:56

iOS开发常用工具

2018-08-09 20:47:41

2018-01-24 15:42:49

命令功能程序

2017-02-21 12:56:21

iOSMac命令

2014-05-13 09:55:13

iOS开发工具

2015-03-03 15:59:25

Android开发属性

2019-04-12 08:10:33

iOS静态分析Xcode

2019-04-26 06:58:56

iOSLLDBXcode

2019-04-18 09:31:07

iOS项目开发断点代码

2014-07-23 13:17:53

iOSUITextField

2016-03-18 09:36:13

ios基础框架

2011-05-11 09:55:18

iOSObjective-C

2013-06-03 16:27:49

iOS开发移动应用移动开发

2015-09-01 10:37:54

ios静态库开发

2013-09-12 12:55:53

iOS开发

2013-09-12 15:37:09

iOS开发流程

2011-05-11 10:02:37

iOS

2013-07-29 04:46:48

iOS开发iOS开发学习iOS小知识

2014-07-21 14:49:35

iOSUILabel
点赞
收藏

51CTO技术栈公众号