iOS开发:#define宏定义

97 阅读2分钟

​本文已参与「新人创作礼」活动,一起开启掘金创作之路。

 1.获取屏幕宽度与高度(横竖屏)

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 // 当前Xcode支持iOS8及以上

#define SCREEN_WIDTH ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width)
#define SCREENH_HEIGHT ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.height)
#define SCREEN_SIZE ([[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)
#else
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_SIZE [UIScreen mainScreen].bounds.size
#endif

2.获取通知中心

#define LRNotificationCenter [NSNotificationCenter defaultCenter]

3.设置随机颜色

#define LRRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]

4.设置RGB颜色/设置RGBA颜色

#define LRRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define LRRGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
// clear背景颜色
#define LRClearColor [UIColor clearColor]

5.判断是否为iPhone

#define kISiPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

6.判断是否为iPad

#define kISiPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

7.用户偏好设置

#define kUserDefaults [NSUserDefaults standardUserDefaults]

8.通知中心

#define kNotificationCenter [NSNotificationCenter defaultCenter]

9.APP版本号

#define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]

10.设备版本号

#define kSystemVersion [[UIDevice currentDevice] systemVersion]

11.获取当前语言

#define kCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])

12.获取沙盒Document路径

#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]

13.获取沙盒temp路

#define kTempPath NSTemporaryDirectory()

14.获取沙盒Cache路径

#define kCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

15.//Debug模式下打印日志,当前行,函数名 并弹出一个警告框

#ifdef DEBUG 
#   define  NSLog(fmt, ...)  { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__]  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; } 
#else 
#   define NSLog(...) 
#endif

16.字符串是否为空

#define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )

17.数组是否为空

#define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)

18.字典是否为空

#define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0)

19.是否是空对象

#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))

20.弱引用/强引用

#define kWeakSelf(type) __weak typeof(type) weak##type = type;

#define kStrongSelf(type) __strong typeof(type) type = weak##type;

21.由角度转换弧度

#define kDegreesToRadian(x) (M_PI * (x) / 180.0)

22.由弧度转换角度

#define kRadianToDegrees(radian) (radian * 180.0) / (M_PI)

//归档解档

#define CODING(self)\

\

-(void)encodeWithCoder:(NSCoder *)aCoder{\

unsigned int count;\

Ivar *list = class_copyIvarList([self class], &count);\

for (int i = 0; i<count; i++) {\

Ivar iv = list[i];\

const char *name = ivar_getName(iv);\

NSString *nameStr = [NSString stringWithUTF8String:name];\

id value = [self valueForKey:nameStr];\

[aCoder encodeObject:value forKey:nameStr];\

}\

free(list);\

NSLog(@"归档%@..",[self class]);\

\

}\

-(instancetype)initWithCoder:(NSCoder *)aDecoder{\

if (self == [super init]) {\

unsigned int count;\

Ivar *list = class_copyIvarList([self class], &count);\

for (int i = 0; i<count; i++) {\

Ivar iv = list[i];\

const char *name = ivar_getName(iv);\

NSString *nameStr = [NSString stringWithUTF8String:name];\

id value = [aDecoder decodeObjectForKey:nameStr];\

[self setValue:value forKey:nameStr];\

}\

free(list);\

}\

NSLog(@"解档%@..",[self class]);\

\

return self;\

}

24.iOS11.0系统下的iPhone-X

#define ScreenWidth [[UIScreen mainScreen] bounds].size.width

#define ScreenHeight [[UIScreen mainScreen] bounds].size.height

#define Is_Iphone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

#define Is_Iphone_X (Is_Iphone && ScreenHeight == 812.0)

#define NaviHeight Is_Iphone_X ? 88 : 64

#define TabbarHeight Is_Iphone_X ? 83 : 49

#define BottomHeight Is_Iphone_X ? 34 : 0