IOS开发中常用的宏定义

ios讨论群1群:135718460

有些时候,我们需要将代码简洁化,这样便于读代码。我们可以将一些不变的东东抽取出来,将变化的东西作为参数。定义为宏,这样在写的时候就简单多了。


下面例举了一些常用的宏定义和大家分享:

1. 判断设备的操作系统是不是ios7

#define IOS7   (  [[[UIDevice currentDevice].systemVersion doubleValue] >= 7.0] )

2. 判断当前设备是不是iPhone5 

#define kScreenIphone5    (([[UIScreen mainScreen] bounds].size.height)>=568)




3.获取当前屏幕的高度
#define kMainScreenHeight ([UIScreen mainScreen].applicationFrame.size.height)

4.获取当前屏幕的宽度

#define kMainScreenWidth  ([UIScreen mainScreen].applicationFrame.size.width)

5.获得RGB颜色

#define SMSColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]


6..自定义Log

#ifdef DEBUG

#define SMSLog(...) NSLog(__VA_ARGS__)

#else

#define SMSLog(...)

#endif


7.单例

// @interface
#define singleton_interface(className) \
+ (className *)shared##className;


// @implementation
#define singleton_implementation(className) \
static className *_instance; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instance = [super allocWithZone:zone]; \
    }); \
    return _instance; \
} \
+ (className *)shared##className \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instance = [[self alloc] init]; \
    }); \
    return _instance; \
}




  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值