美文网首页
iOS常用宏定义大全

iOS常用宏定义大全

作者: 蜗牛锅 | 来源:发表于2018-03-08 10:59 被阅读104次

宏定义与常量的区别

宏:只是在预处理器里进行文本替换,不做任何类型检查,宏能定义代码,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))

相关文章

  • iOS常用宏定义

    打印日志的几种写法 推荐文章iOS 日常工作之常用宏定义大全iOS常用宏定义 结束语 到这里就结束了,如若不懂的话...

  • iOS开发常用宏定义

    iOS 日常工作之常用宏定义大全iOS-OC中常见的一些宏YYCategories 目录 1、长、宽、高2、判断设...

  • iOS常用宏定义大全

    宏定义与常量的区别 宏:只是在预处理器里进行文本替换,不做任何类型检查,宏能定义代码,const不能,多个宏编译时...

  • iOS 常用宏定义大全

    没有一蹴而就的天才,只有不断点亮天赋的勤者

  • iOS 开发小经验

    iOS 开发中你是否遇到这些经验问题(一)iOS 开发中你是否遇到这些经验问题(二)iOS 日常工作之常用宏定义大全

  • iOS-常用宏定义

    [转自:iOS常用宏定义][http://www.cocoachina.com/ios/20161207/1831...

  • iOS 常用宏定义

    iOS 开发中使用一些常用宏定义可以大大提高开发效率,提高代码的重用性.以下是一些常用的宏定义: 像这些宏定义,在...

  • iOS 三方库大全  Unit Test 开始

    iOS三方库大全(常用) iOS三方库宝典(?) IOS 常用的第三方库 iOS常用第三方库大全 iOS三方库大全...

  • IOS NSLog宏定义

    IOS NSLog宏定义 标签(空格分隔): IOS IOS NSLog宏定义 宏定义NSLog方法,不用加";"...

  • iOS常用宏定义

    1.UI元素 //NavBar高度#defineNAVIGATIONBAR_HEIGHT 44//StatusBa...

网友评论

      本文标题:iOS常用宏定义大全

      本文链接:https://www.haomeiwen.com/subject/dniefftx.html