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

May 4th Youth Day丨100,000 meters of youth! How many meters are you at?

Youth is full of vitality Spreading down and taki...

2019, top ten marketing keywords!

In a blink of an eye, 2019 is coming to an end As...

How to organize a successful offline promotion activity?

How do you start to create an offline event? 1. T...

Is 360 information flow advertising effective for men?

As a search engine media advertising platform, 36...

Auntie Wang fried prawns and almost burned her house! How to fry frozen food?

Spring Festival holiday + student winter vacation...

7 essential skills for operations - marketing promotion steps!

A correct marketing promotion process Marketing pr...

Is technology a booster for the Olympics or a disguised form of stimulant?

On August 6, Beijing time, the 31st Olympic Games...

Ten things you must know about the Windows 10 conference

Microsoft launched the consumer preview of Window...

BMW 7 Series flagship model priced over $150,000 announced

Recently, BMW officially announced the price of t...

3000 words to dismantle the replicable private domain traffic gameplay

You must have heard of ChaCha Foods. Yes, the Cha...