iOS常用宏

// 屏幕宽高
#define kScreenW    [UIScreen mainScreen].bounds.size.width
#define kScreenH    [UIScreen mainScreen].bounds.size.height
// 状态栏高
#define kStatusH    [UIApplication sharedApplication].statusBarFrame.size.height
// 导航栏高
#define kNavigationBarH self.navigationController.navigationBar.frame.size.height
// tabbar高
#define kTabBarH    self.tabBarController.tabBar.frame.size.height
// 获得RGB颜色
#define kColorRGB(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
// 获得RGB颜色自定义透明度
#define kColorRGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
// 颜色值宏函数
#define kColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
// 颜色值自定义透明度
#define kColorFromRGBA(rgbValue, a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:a]
// 判断是否为iPhone4
#define kIPhone4 ([UIScreen mainScreen].bounds.size.height == 480)
// 判断是否为iPhone5
#define kIPhone5 ([UIScreen mainScreen].bounds.size.height == 568)
// 判断是否为iPhone6
#define kIPhone6 ([UIScreen mainScreen].bounds.size.height == 667)
// 判断是否为IPhone6Plus
#define kIPhone6Plus ([UIScreen mainScreen].bounds.size.height == 736)
// 判断是哪个版本
#define kIOS(version) ([[UIDevice currentDevice].systemVersion doubleValue] >= version)
#define kIOS8 kIOS(8.0)
#define kIOS9 kIOS(9.0)
#define kIOS10 kIOS(10.0)
// appdelegate
#define kAppDelegate (AppDelegate *)[UIApplication sharedApplication].delegate
// 自定义Log
#ifdef DEBUG  // 调试阶段
#define MYLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else // 发布阶段
#define MYLog(...)
#endif
// 获取系统版本号
#define kSystemVersion [[[UIDevice currentDevice] systemVersion] floatValue]
单例宏
// 1. 解决.h文件
#define singletonInterface(className)          + (instancetype)shared##className;

// 2. 解决.m文件
// 判断 是否是 ARC
#if __has_feature(objc_arc)
#define singletonImplementation(className) \
static id instance; \
+ (instancetype)allocWithZone:(struct _NSZone *)zone { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
instance = [super allocWithZone:zone]; \
}); \
return instance; \
} \
+ (instancetype)shared##className { \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        instance = [[self alloc] init]; \
    }); \
    return instance; \
} \
- (id)copyWithZone:(NSZone *)zone { \
    return instance; \
}
#else
// MRC 部分
#define singletonImplementation(className) \
static id instance; \
+ (instancetype)allocWithZone:(struct _NSZone *)zone { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
instance = [super allocWithZone:zone]; \
}); \
return instance; \
} \
+ (instancetype)shared##className { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
instance = [[self alloc] init]; \
}); \
return instance; \
} \
- (id)copyWithZone:(NSZone *)zone { \
return instance; \
} \
- (oneway void)release {} \
- (instancetype)retain {return instance;} \
- (instancetype)autorelease {return instance;} \
- (NSUInteger)retainCount {return ULONG_MAX;}

#endif

// 提示,最后一行不要使用 \
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS开发过程中,使用的一些常用宏定义 字符串是否为空#define kStringIsEmpty(str) ([...
    goyohol阅读 5,401评论 30 85
  • 字符串是否为空 数组是否为空 字典是否为空 是否是空对象 获取屏幕宽度与高度 ( " \ ":连接行标志,连接上下...
    为什么划船不靠桨阅读 406评论 0 0
  • 宏定义可以很方便开发和调试,我们也要对其进行归类,提高代码可读性和规范性。 宏定义在很多方面都会使用,例如定义高度...
    zzcz_cc阅读 358评论 0 2
  • 第七章:数组 2017.02.27 数组 数组是值的有序集合。每个值叫做一个元素,而每个元素在数组中有一个位置,以...
    静候那一米阳光阅读 444评论 0 1
  • 今天开始我也要在简书平台上写日志了,要把我生活中的点滴都记录上来。写日志的目的很简单一是提升自己的思维逻辑,二是...
    金太日阅读 233评论 2 2