一、objective-C项目pch文件中定义函数和代码块
//定义
#define SendNotification @"SendNotification"
#define sendMessage(msg) \
({\
dispatch_async(dispatch_get_main_queue(), ^{\
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];\
[notificationCenter postNotificationName:SendNotification object:nil userInfo:@{@"msg":msg}];\
});\
})
//使用
sendMessage(@"发个消息试试");
//有返回的宏函数定义
#define getSum(a,b) \
({\
(a+b);\
})
//使用
double sum = getSum(M_PI,M_E);
//定义状态栏高度
#define kStatusBarHeight \
({\
@available(iOS 13.0, *) ? [[[UIApplication sharedApplication] windows] objectAtIndex:0].windowScene.statusBarManager.statusBarFrame.size.height : [[UIApplication sharedApplication] statusBarFrame].size.height;\
})
//使用
kStatusBarHeight
//定义
#define SendNotification @"SendNotification"
#define sendMessage(msg) \
^(){\
dispatch_async(dispatch_get_main_queue(), ^{\
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];\
[notificationCenter postNotificationName:SendNotification object:nil userInfo:@{@"msg":msg}];\
});\
}()
//使用
sendMessage(@"发个消息试试");
//有返回的宏代码块定义
#define getSum(a,b)\
^(){\
return a+b;\
}()
//使用
double sum = getSum(M_PI,M_E);
//定义状态栏高度
#define kStatusBarHeight \
^(){\
if (@available(iOS 13.0, *)) {\
UIStatusBarManager *statusBarManager = [[[UIApplication sharedApplication] windows] objectAtIndex:0].windowScene.statusBarManager;\
return statusBarManager.statusBarFrame.size.height;\
}else{\
return [[UIApplication sharedApplication] statusBarFrame].size.height;\
}\
}()
//使用
kStatusBarHeight
二、swift项目中中定义函数
func kSafeAreaBottom() -> CGFloat {
var kSafeAreaBottom: CGFloat = 0
if #available(iOS 11.0, *) {
kSafeAreaBottom = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
} else {
kSafeAreaBottom = 0
}
return kSafeAreaBottom
}
参考文章
iOS 使用宏定义函数和代码块
网友评论