iOS 常用的宏

#define NSAssert(condition, desc, ...)    \
    do {                \
    __PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
    if (!(condition)) {        \
        [[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd \
        object:self file:[NSString stringWithUTF8String:__FILE__] \
            lineNumber:__LINE__ description:(desc), ##__VA_ARGS__]; \
    }                \
        __PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
    } while(0)

通常用来预处理,condition 是条件,desc 是输出的内容

例如:

 NSAssert([self al_containsMinimumNumberOfViews:1], @"This array must contain at least 1 view.");   // 用来判断数组元素是否大于1,如果大于一则不输出,反之输出



#define SuppressPerformSelectorLeakWarning(Stuff) \

do { \

_Pragma("clang diagnostic push") \

_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \

Stuff; \

_Pragma("clang diagnostic pop") \

} while (0)

去除"-(id)performSelector:(SEL)aSelector withObject:(id)object;"的警告

// 使用案例 列举
SuppressPerformSelectorLeakWarning(
                                           [classViewControllerNavManager performSelector:selNavToSearch withObject:self];
                                           );

// 判断是否是大于7 的系统

#define IsIOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0 ? YES : NO)

// 用来判断指定版本是否大于指定的版本信息

#define LTAPI_IS_ALLOWED(version) ([[[UIDevice currentDevice] systemVersion] doubleValue] >= version)


//设备屏幕大小

#define __MainScreenFrame   [[UIScreen mainScreen] bounds]

//设备屏幕宽

#define __MainScreen_Width  ((__MainScreenFrame.size.width)<(__MainScreenFrame.size.height)?(__MainScreenFrame.size.width):(__MainScreenFrame.size.height))

#define __MainScreen_Height ((__MainScreenFrame.size.height)>(__MainScreenFrame.size.width)?(__MainScreenFrame.size.height):(__MainScreenFrame.size.width))


//自动布局使用
#define kRealWidth(d) (d)*((__MainScreen_Width)/320.0)

通过该方法进行一个具体位置的定位功能


//数组越界检处理
#define OBJECT_OF_ATINDEX(_ARRAY_,_INDEX_) ((_ARRAY_)&&[_ARRAY_ isKindOfClass:[NSArray class]]&&(_INDEX_ < [_ARRAY_ count])&&(_INDEX_ >= 0)?([_ARRAY_ objectAtIndex:_INDEX_]):(nil))//有返回值


今后项目里还会陆续补充常用的一些宏的使用。。。。。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值