We all know that macros are not only convenient, but also improve development efficiency. The following is a summary of some common macros used in iOS development that will continue to be added.

// Whether the string is empty
#define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
// Whether the array is empty
#define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
// Whether the dictionary is empty
#define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)
// Is an empty object
#define kObjectIsEmpty(_object) (_object == nil \
|| [_object isKindOfClass:[NSNull class]] \
|| ([_object respondsToSelector:@selector(length)] && [(NSData *)_object length] == 0) \
|| ([_object respondsToSelector:@selector(count)] && [(NSArray *)_object count] == 0))

// Get the screen width and height
#define kScreenWidth \
([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.width)
#define kScreenHeight \
([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.height)
#define kScreenSize \
([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? CGSizeMake([UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale,[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale) : [UIScreen mainScreen].bounds.size)

// Some abbreviations
#define kApplication        [UIApplication sharedApplication]
#define kKeyWindow          [UIApplication sharedApplication].keyWindow
#define kAppDelegate        [UIApplication sharedApplication].delegate
#define kUserDefaults       [NSUserDefaults standardUserDefaults]
#define kNotificationCenter [NSNotificationCenter defaultCenter]

/ / APP version number
#define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
// System version number
#define kSystemVersion [[UIDevice currentDevice] systemVersion]
// Get the current language
#define kCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
// Check whether it is iPhone
#define kISiPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
// Check whether it is iPad
#define kISiPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

// Get the sandbox Document path
#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
// Get the sandbox temp path
#define kTempPath NSTemporaryDirectory()
// Get the sandbox Cache path
#define kCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

// Check whether it is a real machine or an emulator
#if TARGET_OS_IPHONE
/ / real machine
#endif

#if TARGET_IPHONE_SIMULATOR
/ / simulator
#endif

// NSLog printed at development time, but not at publish time
#ifdef DEBUG
#define NSLog(...) NSLog(@"%s line %d \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define NSLog(...)
#endif

/ / color
#defineKRGBColor (r, G, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#defineKRGBAColor (r, g, B, a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]
#define kRandomColor           kRGBColor(arc4random_uniform(256),arc4random_uniform(256),arc4random_uniform(256))

#defineKColorWithHex (rgbValue) \ [UIColor colorWithRed:(float)((rgbValue & 0xFF0000) >> 16)) / 255.0 \ Green :(float)((rgbValue & 0xFF00) >> 8)) / 255.0 \ Blue :(float)(rgbValue & 0xFF)) / 255.0 alpha:1.0]

// Weak reference/strong reference
#define kWeakSelf(type)   __weak typeof(type) weak##type = type;
#define kStrongSelf(type) __strong typeof(type) type = weak##type;

// Convert radians from angles
#defineDegreestoradian (x) (M_PI * (x) / 180.0)
// Convert the Angle from radians
#defineKradiantodegree (Radian) (Radian * 180.0)/(M_PI)

// Get an interval
#define kStartTime CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
#define kEndTime   NSLog(@"Time: %f", CFAbsoluteTimeGetCurrent() - start)

Copy the code

Xiaobian this, to recommend you an excellent iOS communication platform, platform partners are very excellent iOS developers, we focus on the sharing of technology and skills exchange, we can discuss technology on the platform, exchange and learning. Welcome to join (want to enter the small series of QQ). Author: 18173184023 iOS_ komatsu elder brother link: www.jianshu.com/p/be00c3f3c… Source: Jane Book