ios 常用的宏定义

#pragma mark - shortcuts
#ifdef QA
# define NSLog(fmt, ...) NSLog((@"[文件名:%s]\n" "[函数名:%s]\n" "[行号:%d] \n" fmt), __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
# define NSLog(...){};
#endif


//判断系统是否大于等于v
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

//表示释放对象,并且将该对象赋值为nil
#define RELEASE_SAFELY(__POINTER) { [__POINTER release]; __POINTER = nil; }

// Release a CoreFoundation object safely.检测对象是否为nil,如果不为nil释放对象,最后赋值为nil。
#define RELEASE_CF_SAFELY(__REF) {if (nil != (__REF)) { CFRelease(__REF); __REF = nil; } }

//CFBundleVersion,标识(发布或未发布)的内部版本号。这是一个单调增加的字符串,包括一个或多个时期分隔的整数。
#define HFAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]

//CFBundleShortVersionString  标识应用程序的发布版本号。该版本的版本号是三个时期分隔的整数组成的字符串。第一个整数代表重大修改的版本,如实现新的功能或重大变化的修订。第二个整数表示的修订,实现较突出的特点。第三个整数代表维护版本。该键的值不同于“CFBundleVersion”标识。
#define HFAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]

#define ImageNamed(_pointer) [UIImage imageNamed:_pointer]]


#pragma mark - common functions 

#define RELEASE_SAFELY(__POINTER) { [__POINTER release]; __POINTER = nil; }




#pragma mark - iphone 5 detection functions
#define SCREEN_HEIGHT_OF_IPHONE5 568


#define is4InchScreen() ([UIScreen mainScreen].bounds.size.height == SCREEN_HEIGHT_OF_IPHONE5)

#define Iphone4ch  (is4InchScreen() ? 10 :  100)




#pragma mark - degrees/radian functions 
#define degreesToRadian(x) (M_PI * (x) / 180.0)
#define radianToDegrees(radian) (radian*180.0)/(M_PI)



#pragma mark - color functions 

#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]

#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]

// 定义RGB宏函数
#define RGB(R,G,B) [UIColor colorWithRed:R/255.0f green:G/255.0f blue:B/255.0f alpha:1.0f]

#define PADICTIONARY_VALUE_NAME(a,b) [NSDictionary dictionaryWithObjectsAndKeys:a,@"value",b,@"name",nil]

#define AppVersion            [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]

// 判断是否越狱
#define IS_JailBreak            system("ls") == 0 ? YES : NO

// 机器语言
#define HXApplicationLanguage   [[NSUserDefaults standardUserDefaults] \
objectForKey:@"AppleLanguages"]                                    \
objectAtIndex : 0];

// ios版本
#define IOSVersion          [[[UIDevice currentDevice] systemVersion] floatValue]
#define NEW(CLASS_NAME)  [[CLASS_NAME alloc]init]
#define HFAlert(fmt, ...)   \
{                                       \
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:fmt,##__VA_ARGS__] delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil] ; \
[alert1 show];               \
}

#define HFAlert_T_M_BT(t, m, bt) \
{                                                     \
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:t message:mdelegate:nil cancelButtonTitle:bt otherButtonTitles:nil] ; [alert1 show]; \
}

//打印宏
#define __toString(x) __toString_0(x)
#define __toString_0(x) #x
#define LOG_MACRO(x) NSLog(@"%s=\n%s", #x, __toString(x))

//NSAsset 宏
//不使用NSAssert,因为debug模式会导致循环引用
#undef NSAssert
#define NSAssert(condition, desc, ...)    if(desc) {NSCAssert(condition, desc, ##__VA_ARGS__);} else {NSCAssert(condition, @"%s:%d", __func__, __LINE__);}

在苹果的SDK中可以看到这两个都是定义的宏
NSAssert 的定义如下:
#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)  
#endif  

NSCassert的定义如下:
#define NSCAssert(condition, desc, ...) \  
do {                \  
__PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \  
if (!(condition)) {     \  
    [[NSAssertionHandler currentHandler] handleFailureInFunction:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \  
    file:[NSString stringWithUTF8String:__FILE__] \  
        lineNumber:__LINE__ description:(desc), ##__VA_ARGS__]; \  
}               \  
    __PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \  
} while(0)  
#endif  
小心使用NSAssert,可以看到它的定义中出现了一个self,有可能在你的block中你会发现你明明没有self的strong引用,但是仍然出现了循环引用。就看看你是否使用了NSAssert,这个宏被展开之后持有了self,那么有可能就会出现引用不释放的问题。
而使用NSCassert,就不会有这样的问题了。因为它定义使用的handleFailureInFunction函数,并没有self引用。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值