iOS常用宏定义大全

宏定义与常量的区别

宏:只是在预处理器里进行文本替换,不做任何类型检查,宏能定义代码,const不能,多个宏编译时间相对较长,影响开发效率,调试过慢,const只会编译一次,缩短编译时间。
所以在使用的时候,最好把代码和一些基本数据类型如int抽成宏。
而对于常量字符串使用const,苹果也是这样使用的。

下面总结一下一些常用的宏定义。
1.获取主屏幕和屏幕宽度与高度

OC

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width #define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height #define MAIN_SCREEN [UIScreen mainScreen] 

Swift

let SCREEN_WIDTH = UIScreen.mainScreen().bounds.width
let SCREENH_HEIGHT = UIScreen.mainScreen().bounds.height
let MAIN_SCREEN = UIScreen.mainScreen()

如果支持横屏可以用下面的宏:
OC

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 // 当前Xcode支持iOS8及以上
#define SCREEN_WIDTH ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width) #define SCREENH_HEIGHT ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.height) #define SCREEN_SIZE ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?CGSizeMake([UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale,[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale):[UIScreen mainScreen].bounds.size) #else #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width #define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height #define SCREEN_SIZE [UIScreen mainScreen].bounds.size #endif 

Swift



 #if (__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000  // 当前Xcode支持iOS8及以上
let SCREEN_WIDTH = (MAIN_SCREEN.respondsToSelector(#selector(UIScreen.nativeBounds)) ? MAIN_SCREEN.nativeBounds.width/MAIN_SCREEN.nativeScale : MAIN_SCREEN.bounds.width)
let SCREENH_HEIGHT = (MAIN_SCREEN.respondsToSelector(#selector(UIScreen.nativeBounds)) ? MAIN_SCREEN.nativeBounds.height/MAIN_SCREEN.nativeScale : MAIN_SCREEN.bounds.height) let SCREEN_SIZE = (MAIN_SCREEN.respondsToSelector(#selector(UIScreen.nativeBounds)) ? CGSizeMake(SCREEN_WIDTH, SCREENH_HEIGHT) : MAIN_SCREEN.bounds.size) #else let SCREEN_WIDTH = UIScreen.mainScreen().bounds.width let SCREENH_HEIGHT = UIScreen.mainScreen().bounds.height let SCREEN_SIZE = UIScreen.mainScreen().bounds.size #endif 

2.获取通知中心和UserDefaults

OC

#define DENotificationCenter [NSNotificationCenter defaultCenter]
#define SDUserDefaults [NSUserDefaults standardUserDefaults] 

Swift

let DENotificationCenter = NSNotificationCenter.defaultCenter()
let SDUserDefaults = NSUserDefaults.standardUserDefaults()

3.设置随机颜色

OC

#define GRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]

Swift

let GRandomColor = UIColor(red: CGFloat(arc4random_uniform(256)/255), green: CGFloat(arc4random_uniform(256)/255), blue: CGFloat(arc4random_uniform(256)/255), alpha: 1) 

4.设置RGB颜色/设置RGBA颜色

OC

#define RGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define RGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a] // clear背景颜色 #define GClearColor [UIColor clearColor] 

Swift

func colorWith(red red : CGFloat,green : CGFloat,blue:CGFloat) -> UIColor {
    return UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: 1) } func colorWith(red red : CGFloat,green : CGFloat,blue:CGFloat,a:CGFloat) -> UIColor { return UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: a) } 

5.自定义高效率的 NSLog

项目开发中,我们会在许多地方加上Log,但是发布的时候又不想用这些Log,我们也不可能一个一个的删除,所以自定义Log是必然的!
OC

#ifdef DEBUG
#define debugLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__]) #else #define debugLog(...) #endif 

Swift

func debugLog<T>(message: T,
              file: String = #file,
              method: String = #function,
              line: Int = #line)
{
    #if DEBUG
    print("\((file as NSString).lastPathComponent)[\(line)], \(method): \(message)") #endif } 

6.弱引用/强引用

#define LRWeakSelf(type)  __weak typeof(type) weak##type = type;
#define LRStrongSelf(type) __strong typeof(type) type = weak##type; 

7.由角度转换弧度 由弧度转换角度

#define RadianFrom(degree) (M_PI * (degree) / 180.0)
#define DegreeFrom(radian) (radian*180.0)/(M_PI) 

8.获取UIImage

#define GUIImageFor(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]

9.获取当前语言

#define LRCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0]) 

10.使用 ARC 和 MRC

#if __has_feature(objc_arc)
// ARC
#else // MRC #endif 

11.判断当前的iPhone设备/系统版本

OC

//判断是否为iPhone
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
//判断是否为iPad #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //判断是否为ipod #define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"]) // 判断是否为 iPhone 5SE #define iPhone5SE [[UIScreen mainScreen] bounds].size.width == 320.0f && [[UIScreen mainScreen] bounds].size.height == 568.0f // 判断是否为iPhone 6/6s #define iPhone6_6s [[UIScreen mainScreen] bounds].size.width == 375.0f && [[UIScreen mainScreen] bounds].size.height == 667.0f // 判断是否为iPhone 6Plus/6sPlus #define iPhone6Plus_6sPlus [[UIScreen mainScreen] bounds].size.width == 414.0f && [[UIScreen mainScreen] bounds].size.height == 736.0f //获取系统版本 #define IOS_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue] //判断 iOS 8 或更高的系统版本 #define IOS_VERSION_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0)? (YES):(NO)) 

Swift



//判断是否为iPhone
let IS_IPHONE = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Phone)
//判断是否为iPad
let IS_IPAD = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad) //判断是否为ipod let IS_IPOD = (UIDevice.currentDevice().model == "iPod touch") // 判断是否为 iPhone 5SE let is_iPhone5SE = (UIScreen.mainScreen().bounds.width == 320.0 && UIScreen.mainScreen().bounds.height == 568.0) // 判断是否为iPhone 6/6s let is_iPhone6Or6s = (UIScreen.mainScreen().bounds.width == 375.0 && UIScreen.mainScreen().bounds.height == 667.0) // 判断是否为iPhone 6Plus/6sPlus let is_iPhone6PlusOr6sPlus = (UIScreen.mainScreen().bounds.width == 414.0 && UIScreen.mainScreen().bounds.height == 736.0) //获取系统版本 let IOS_SYSTEM_VERSION = Float(UIDevice.currentDevice().systemVersion)! //判断 iOS 8 或更高的系统版本 let IS_IOS_VERSION_8_OR_LATER = ((IOS_SYSTEM_VERSION >= 8.0) ? (true):(false)) 

12.判断是真机还是模拟器

#if TARGET_OS_IPHONE
//iPhone Device
#endif #if TARGET_IPHONE_SIMULATOR //iPhone Simulator #endif 

13.沙盒目录文件

//获取temp
#define kPathTemp NSTemporaryDirectory()
//获取沙盒 Document #define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] //获取沙盒 Cache #define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] 

14.GCD
OC

//GCD - 只执行一次
#define kDISPATCH_ONCE(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);
//GCD - 在Main线程上运行 #define kDISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock); //GCD - 开启异步线程 #define kDISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl); 

Swift

func kDISPATCH_ONCE(onceBlock : dispatch_block_t) { var onceToken = dispatch_once_t() dispatch_once(&onceToken, onceBlock) } func kDISPATCH_MAIN_THREAD(mainQueueBlock : dispatch_block_t) { dispatch_async(dispatch_get_main_queue(), mainQueueBlock) } 

键值提示

#define GKeyPath(objc,keyPath) @(((void)objc.keyPath,#keyPath)) 

//弃用的方法后缀

#define TEMPORARILY_ABANDONED __attribute__((deprecated))

转载于:https://www.cnblogs.com/Free-Thinker/p/11175641.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值