发现项目中有inline,以前没接触过,所以查了些资料,
内联函数与宏定义区别
(1)内联函数在编译时展开,宏在预编译时展开;
(2)内联函数直接嵌入到目标代码中,宏是简单的做文本替换;
(3)内联函数有类型检测、语法判断等功能,而宏没有;
(4)inline函数是函数,宏不是;
(5)宏定义时要注意书写(参数要括起来)否则容易出现歧义,内联函数不会产生歧义;
具体的文章地址: https://blog.csdn.net/licui94/article/details/8134290
下面是一些常用的内联函数
1.当前时间
CG_INLINE NSString * dateStr(){
NSDate *currentDate = [NSDate date];//获取当前时间,日期
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:currentDate];
return dateString;
}
2.字体高度
CG_INLINE CGFloat calculateRowHeight(NSString * string, NSInteger fontSize, CGFloat width){
NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};//指定字号
CGRect rect = [string boundingRectWithSize:CGSizeMake(width, 0)/*计算高度要先指定宽度*/ options:NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading attributes:dic context:nil];
return rect.size.height;
}
3.自定义返回按钮
CG_INLINE UIButton *CMBackButton(id target, SEL action) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
NSDictionary *dict = [[UIBarItem appearance] titleTextAttributesForState:UIControlStateNormal];
button.titleLabel.font = dict[NSFontAttributeName];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
return button;
}
4.拼接请求url
#define TestHost @"http://www.baidu.com"
CG_INLINE NSString * appendURL(NSString * subUrl){
return [TestHost stringByAppendingString:subUrl];
}
常用的宏
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height