iOS 常用的define合集

本文介绍了在Swift中使用宏定义来简化代码编写的方法,包括设备UUID获取、应用版本号读取、图片加载、弱引用创建、AppDelegate访问、状态栏高度获取、iPhoneX判断、屏幕尺寸获取、颜色定义及各类空值判断。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值