IOS开发中常用的宏,做下记录,备忘下。
1、设置view的圆角边框
#define LRViewBorderRadius(View, Radius, Width, Color)\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]
调用很简单:
LRViewBorderRadius(myView, 20.0f, 1.0f, [UIColor blackColor]);
2、 设置view任意方向的圆角边框
#define MotionViewBorderRadius(View, Radius, direction, Color)\
[View.layer setMasksToBounds:YES];\
CAShapeLayer *maskLayer = [CAShapeLayer layer];\
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:View.bounds byRoundingCorners: direction cornerRadii: (CGSize){Radius, Radius}].CGPath;\
[View.layer setMask:maskLayer];\
[View.layer setBorderColor:[Color CGColor]]
调用:
MotionViewBorderRadius(myView, 20.0f, UIRectCornerTopLeft | UIRectCornerTopRight, [UIColor blackColor]);
3、 获取图片资源
#define kGetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]
调用很方便的说:
myView.image = kGetImage(@"game_icon");
4、获取当前语言
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
5、打印Log
#ifdef DEBUG //处于开发阶段
#define debugLog(s, ... ) NSLog( @"[%@:in line: %d]-->[message: %@]", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#define debugMethod() NSLog(@"%s", __func__)
#else //处于发布阶段
#define debugLog(s, ... )
#define debugMethod()
#endif
6、处理循环引用问题——weakSelf
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;
调用方便:
WS(we)//不想用weakSelf,用其他字符替代,譬如:we
we.subView.backgroundColor = [UIColor blackColor];
7、 宏与const 的使用
很多小伙伴在定义一个常量字符串,都会定义成一个宏,最典型的例子就是接口地址URL、私钥等,在此所有用宏定义常量字符的小伙伴以后就用const来定义吧!为什么呢 ?我们看看:
宏的用法:1、字符串抽成宏 2、代码段抽成宏使用(譬如上面的1到6)。
const用法:1、字符串定义成const(对于常量字符串苹果推荐我们使用const)。
1.编译时刻不同,宏属于预编译 ,const属于编译时刻;多个宏对于编译会相对时间较长,影响开发效率,调试过慢,const只会编译一次,缩短编译时间。
2.宏不会检查错误,const会检查错误(const常量的地方若是出错了,至少还能看到名字,这样多少有点提示,用宏的话就没有了。)
3.宏能定义代码,const不能。(也就是上面的1到6不能用const的原因)
通过以上对比,不难知道我们以后在开发中如果定义一个常量字符串就用const,定义代码就用宏吧。
ps:至于const与static和extern的配合使用,下次会专门弄一篇来介绍下。
8、单例
怎么能忘了我们常用的单例大大呢?
//帮助实现单例设计模式(ARC与非ARC)
//实现.h文件
#define SingletonH(methodName) + (instancetype)Shared##methodName;
//实现.m文件
#if __has_feature(objc_arc) //是ARC
#define SingletonM(methodName) \
static id _instance;\
+ (instancetype)allocWithZone:(struct _NSZone *)zone{\
if (_instance ==nil) {\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
}\
return _instance;\
}\
+(instancetype)Shared##methodName{\
return [[self alloc]init];\
}\
- (id)copyWithZone:(struct _NSZone *)zone{\
return _instance;\
}\
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instance; \
} \
- (id)mutableCopyWithZone:(struct _NSZone *)zone{\
return _instance;\
}
#else// 非ARC
#define SingletonM(methodName) \
static id _instance;\
+ (instancetype)allocWithZone:(struct _NSZone *)zone{\
if (_instance ==nil) {\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
}\
return _instance;\
}\
+(instancetype)Shared##methodName{\
return [[self alloc]init];\
}\
-(oneway void)release{\
\
}\
-(instancetype)retain{\
return self;\
}\
- (id)autorelease \
{ \
return _instance; \
} \
-(NSUInteger)retainCount{\
return 1;\
}\
- (id)copyWithZone:(struct _NSZone *)zone{\
return _instance;\
}\
- (id)mutableCopyWithZone:(struct _NSZone *)zone{\
return _instance;\
}\
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instance; \
}
#endif
之后你只需要调用简单的两句代码,就给你一个单例了。
SingletonH(methodName)
SingletonM(methodName)
好了,暂时更新到这里,有好用的宏,可以给我留言哈。不做伸手党。