Objective-C常用的宏定义
2016-03-08 10:02
387 查看
首先宏定义学习传送门:宏定义的黑魔法 - 宏菜鸟起飞手册
然后介绍一些开发中常用到的宏定义,个人总结,欢迎补充:
快速weak:
颜色:
屏幕宽高:
系统版本:
图片:
当前语言:
字符串拼接:
TODO:
然后介绍一些开发中常用到的宏定义,个人总结,欢迎补充:
快速weak:
#define WEAKOBJ(weakObj) __weak __typeof(&*weakObj)weak_##weakObj = weakObj; #define WEAKSELF(weakSelf) __weak __typeof(&*self)weakSelf = self;
颜色:
#define UIColorFromRGBA(rgbValue, alphaValue) \ [UIColor \ colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \ green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \ blue:((float)(rgbValue & 0x0000FF))/255.0 \ alpha:alphaValue]
屏幕宽高:
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
系统版本:
#define CURRENT_IOS_VERISON [[[UIDevice currentDevice] systemVersion] floatValue] #define NAVIGATIONBAR_HEIGHT (CURRENT_IOS_VERISON >= 7 ? 64.0f:44.0f) #define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame) #define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending) #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending) #define IS_OS_5_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0") #define IS_OS_6_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0") #define IS_OS_7_OR_LATER SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")
图片:
//读取本地图片 #define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]] //定义UIImage对象 #define IMAGE(A) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:A ofType:nil]] //定义UIImage对象 #define ImageNamed(_pointer) [UIImage imageNamed:_pointer] //可拉伸的图片 #define ResizableImage(name,top,left,bottom,right) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right)] #define ResizableImageWithMode(name,top,left,bottom,right,mode) [[UIImage imageNamed:name] resizableImageWithCapInsets:UIEdgeInsetsMake(top,left,bottom,right) resizingMode:mode]
当前语言:
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
字符串拼接:
#define GETSTRING_WITH(x,y) [NSString stringWithFormat:@"%@%@",x,y]
TODO:
/// 转成字符串 #define STRINGIFY(S) #S // 需要解两次才解开的宏 #define DEFER_STRINGIFY(S) STRINGIFY(S) #define PRAGMA_MESSAGE(MSG) _Pragma(STRINGIFY(message(MSG))) // 为warning增加更多信息 #define FORMATTED_MESSAGE(MSG) "[TODO-" DEFER_STRINGIFY(__COUNTER__) "] " MSG " \n" DEFER_STRINGIFY(__FILE__) " line " DEFER_STRINGIFY(__LINE__) // 使宏前面可以加@ #define KEYWORDIFY try {} @catch (...) {} // 最终使用的宏 #define TODO(MSG) KEYWORDIFY PRAGMA_MESSAGE(FORMATTED_MESSAGE(MSG))
相关文章推荐
- rcnn学习笔记:Rich feature hierarchies for accurate object detection and semantic segmentation
- NSNumber各类型包装转换
- 单例在ObjectiveC中的正确打开方式
- Object-C高级编程读书笔记(1)——Block的基本概念
- Objective-C与Swift中通过字符串获取类的方法对比与实现
- iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式
- 使用C、C++、Objective-C三语言混编时的注意事项
- The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?...
- Runtime of Objective-C
- Objective-C Runtime 一小时入门教程(下)
- Objective-C Runtime 一小时入门教程(中)
- Objective-C Runtime 一小时入门教程(上)
- objective-c 等待多个异步函数完成后,再执行某段代码--GCD Group 的使用
- Objective-C利用正则表达式进行搜索时正则关键字的转义
- Objective-C用正则表达式判断字符串是否为手机号、邮箱、身份证号
- 【转】The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?...
- Objective-C Runtime 一小时入门教程(下)
- Objective-C页面消失或出现时,判断是pop还是push操作
- Objective-C Runtime 一小时入门教程(中)
- new Object[5]语句是否创建了5个对象