常用宏

[TOC]

单例化一个类
// @interface
#define singleton_interface(className) \
+ (className *)shared##className;

// @implementation
#define singleton_implementation(className) \
static className *_instance; \
+ (id)allocWithZone:(NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
+ (className *)shared##className \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
}
从storyboard中取出控制器
#define ViewControllerFromSB(sb,ident) [[UIStoryboard storyboardWithName:sb bundle:nil] instantiateViewControllerWithIdentifier:ident]
尺寸、适配
//状态栏高度
#define STATUS_BAR_HEIGHT 20
//NavBar高度
#define NAVIGATION_BAR_HEIGHT 44
//状态栏 + 导航栏 高度
#define STATUS_AND_NAVIGATION_HEIGHT ((STATUS_BAR_HEIGHT) + (NAVIGATION_BAR_HEIGHT))

// 宽高
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
// 适配
#define LayoutW(w) [[UIScreen mainScreen] bounds].size.width / 375 * w
#define LayoutH(h)  ([[UIScreen mainScreen] bounds].size.height > 667? [[UIScreen mainScreen] bounds].size.height : 667) / 667 * h
颜色值
//颜色值
#define COLOR_RGB(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]

#define RGB(__r, __g, __b)  [UIColor colorWithRed:(1.0*(__r)/255)\
green:(1.0*(__g)/255)\
blue:(1.0*(__b)/255)\
alpha:1.0]

#define RGBA(__r, __g, __b, __a)  [UIColor colorWithRed:(1.0*(__r)/255)\
green:(1.0*(__g)/255)\
blue:(1.0*(__b)/255)\
alpha:__a]
获取系统时间戳
//获取系统时间戳
#define GET_CurentTime [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]]
手机系统
// 当前应用软件版本
#define GET_APPCURVERSION [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]

// 获取手机系统版本
#define GET_iOSVERSION [[[UIDevice currentDevice] systemVersion] floatValue]

#define IOS10_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0)
#define IOS9_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)
#define IOS8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#define IOS7_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

// 获取手机序列号
#define GET_UUID [[UIDevice currentDevice] uniqueIdentifier]
沙盒
//文件目录
#define kPathTemp                   NSTemporaryDirectory()
#define kPathDocument               [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
#define kPathCache                  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]
#define kPathSearch                 [kPathDocument stringByAppendingPathComponent:@"Search.plist"]

#define kPathMagazine               [kPathDocument stringByAppendingPathComponent:@"Magazine"]
#define kPathDownloadedMgzs         [kPathMagazine stringByAppendingPathComponent:@"DownloadedMgz.plist"]
#define kPathDownloadURLs           [kPathMagazine stringByAppendingPathComponent:@"DownloadURLs.plist"]
#define kPathOperation              [kPathMagazine stringByAppendingPathComponent:@"Operation.plist"]

#define kPathSplashScreen           [kPathCache stringByAppendingPathComponent:@"splashScreen"]
字体
// 字体
#define FONT(frontSize) [UIFont systemFontOfSize:frontSize]
#define FRONTWITHSIZE(frontSize) [UIFont fontWithName:@"MicrosoftYaHei" size:frontSize]

#define FONT_NAME_SIZE(name,frontSize) [UIFont fontWithName:name size:frontSize]
处理nil
//去掉nsstring的空格
#define NSStringRemoveSpace(string) [string stringByReplacingOccurrencesOfString:@" " withString:@""]

//处理nil
#define NSStringIsNil(STR) STR ? STR: @""

#define IsNilOrNull(_ref)   (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]))
弱引用
// 弱引用
#define WEAK_SELF __weak typeof(self) weakSelf = self
解档归档
//归档到Library/Cache文件夹
#define NSArchiveToFile(obj,fileName) \
NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];\
NSString *guiDangPathStr = [NSString stringWithFormat:@"%@/GuiDangCache", pathDocuments];\
NSFileManager *fileManager = [NSFileManager defaultManager];\
if (![fileManager fileExistsAtPath:guiDangPathStr]) {\
[fileManager createDirectoryAtPath:guiDangPathStr withIntermediateDirectories:YES attributes:nil error:nil];\
}\
NSString *guiDangpath = [guiDangPathStr stringByAppendingPathComponent:fileName];\
[NSKeyedArchiver archiveRootObject:obj toFile:guiDangpath];

//解档
#define NSUnarchiveFromFile(fileName) [NSKeyedUnarchiver unarchiveObjectWithFile: [[NSString stringWithFormat:@"%@/GuiDangCache", [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]] stringByAppendingPathComponent:fileName]];
DEBUG打印
//DEBUG  模式下打印日志,当前行
#ifdef DEBUG
#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define DLog(...)
#endif

推荐阅读更多精彩内容

  • iOS开发过程中,使用的一些常用宏定义 字符串是否为空#define kStringIsEmpty(str) ([...
    goyohol阅读 4,243评论 29 84
  • ifndef MacroDefinition_h define MacroDefinition_h //-----...
    灰客阅读 43评论 0 0
  • //屏幕宽度:#define kSCREENWIDTH [[UIScreen mainScreen] bound...
    瀚宇悟道阅读 68评论 0 0
  • 道骨仙风云高树, 不见仙人此山游。 长生不老广宫寒, 何不教我炼仙丹?
    蔚蓝鹏程作惜别阅读 128评论 6 3
  • 在广州各大出版社了解到,如果想要自费出版书籍,以十多万字为例,从严格审核稿件,到纠正文字标点错误,再到初步审核通过...
    东方侠影阅读 246评论 4 23