1、获取设备的UUID
#define deviceUID [[[UIDevice currentDevice] identifierForVendor] UUIDString]
2、应用商店版本号
#define APPVERSION [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
3、UIImage图片
#define IMAGENAMED(imageName) [UIImage imageNamed:imageName]
4、weakSelf
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;
5、AppDelegate
#define INSTANCEAPP(a) AppDelegate * a = (AppDelegate *)([UIApplication sharedApplication].delegate)
6、状态栏高度
#define kStatusBarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height)
7、iPhoneX判断
#define kDevice_Is_iPhoneX (kStatusBarHeight > 20.0f)
#define topHeight_iPhoneX (kDevice_Is_iPhoneX ? 88 : 64)
#define bottomHeight_iPhoneX (kDevice_Is_iPhoneX ? 34 : 0)
8、屏幕的宽高
#define MainScreenHeight [UIScreen mainScreen].bounds.size.height
#define MainScreenWidth [UIScreen mainScreen].bounds.size.width
9、定义Color
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
#define HEXCOLOR(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]
10、空值判断
#define kIsEmptyString(s) (s == nil || [s isKindOfClass:[NSNull class]] || ([[s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) || ([s isKindOfClass:[NSString class]] && s.length == 0) || [s isEqualToString:@"(null)"])
#define kIsEmptyObject(obj) (obj == nil || [obj isKindOfClass:[NSNull class]] || [obj isEqual:@""])
#define kIsEmptyArray(array) ((array == nil || [array isKindOfClass:[NSNull class]])||(array.count<= 0))
#define kIsDictionary(objDict) (objDict != nil && [objDict isKindOfClass:[NSDictionary class]])
#define kIsArray(objArray) (objArray != nil && [objArray isKindOfClass:[NSArray class]])
本文介绍了在Swift中使用宏定义来简化代码编写的方法,包括设备UUID获取、应用版本号读取、图片加载、弱引用创建、AppDelegate访问、状态栏高度获取、iPhoneX判断、屏幕尺寸获取、颜色定义及各类空值判断。

3794

被折叠的 条评论
为什么被折叠?



