Common macros for iOS development

Common macros for iOS development

[[185940]]

Everyone knows that using macros is not only convenient, but also can improve development efficiency. The following summarizes some commonly used macros in the iOS development process, and will continue to add to it.

  1. // Is the string empty?
  2. #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
  3. // Is the array empty?
  4. #define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array. count == 0)
  5. //Is the dictionary empty?
  6. #define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
  7. //Is it an empty object?
  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. //Get screen width and height
  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. //Some abbreviations
  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 version number
  29. #define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@ "CFBundleShortVersionString" ]
  30. //System version number
  31. #define kSystemVersion [[UIDevice currentDevice] systemVersion]
  32. //Get the current language
  33. #define kCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
  34. //Judge whether it is an iPhone
  35. #define kISiPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  36. //Judge whether it is iPad
  37. #define kISiPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  38.   
  39. //Get the sandbox Document path
  40. #define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
  41. //Get the sandbox temp path
  42. #define kTempPath NSTemporaryDirectory()
  43. //Get the sandbox cache path
  44. #define kCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
  45.   
  46. //Judge whether it is a real device or a simulator
  47. #if TARGET_OS_IPHONE
  48. //Real machine
  49. #endif
  50.   
  51. #if TARGET_IPHONE_SIMULATOR
  52. //Simulator
  53. #endif
  54.   
  55. //NSLog that prints during development but not during release
  56. #ifdef DEBUG
  57. #define NSLog(...) NSLog(@ "%s line %d \n %@\n\n" ,__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
  58. # else  
  59. #define NSLog(...)
  60. #endif
  61.   
  62. //color
  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. //Weak reference/strong reference
  73. #define kWeakSelf(type) __weak typeof(type) weak##type = type;
  74. #define kStrongSelf(type) __strong typeof(type) type = weak##type;
  75.   
  76. //Convert from degrees to radians
  77. #define kDegreesToRadian(x) (M_PI * (x) / 180.0)
  78. //Convert from radians to degrees
  79. #define kRadianToDegrees(radian) (radian * 180.0) / (M_PI)
  80.   
  81. //Get a time interval
  82. #define kStartTime CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
  83. #define kEndTime NSLog(@ "Time: %f" , CFAbsoluteTimeGetCurrent() - start)

<<:  Android Date Time Picker

>>:  Withstand 10 billion requests! Make a "sure" red envelope system

Recommend

After spending money, how can we judge the effectiveness of promotion?

You know that half of your advertising budget is ...

WeChat teaches you how to set up a street stall: This score is your signboard

It seemed as if overnight, setting up stalls beca...

Analysis of 6 popular brand marketing cases!

Internet restructuring, new moves for brands. Viy...

Practical methodology for Douyin e-commerce promotion!

Since Suning.com launched the "100-day battl...

Who will win the battle of words? ——Front-end and back-end passionate debate

Are you still worried about having nowhere to dis...

Pharmacy agency operation implementation plan and model

With the continuous development of China's ph...

iOS 8 has a penetration rate of 82%, while Android 5.0 has only 1.6%.

According to Apple's App Store support page f...

It’s called a ship, but where are the wheels of the ship?

Why do we always call ships "ships"? Do...

Scientists unveil Ebola virus's 'replication machine'

In September this year, the Ebola outbreak reappe...