zl程序教程

您现在的位置是:首页 >  其他

当前栏目

iOS开发常用宏

2023-03-31 11:10:02 时间

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