iOS开发中常用的宏

2019-10-22 16:53:39 浏览数 (1)

常用的宏

打印日志

代码语言:javascript复制
//DEBUG  模式下打印日志,当前行  
#ifdef DEBUG  
#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);  
#else  
#define DLog(...)  
#endif
代码语言:javascript复制
//重写NSLog,Debug模式下打印日志和当前行数  
#if DEBUG  
#define NSLog(FORMAT, ...) fprintf(stderr,"nfunction:%s line:%d content:%sn", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);  
#else  
#define NSLog(FORMAT, ...) nil  
#endif

APP信息

代码语言:javascript复制
//版本号
#define kVersion_Coding [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]
#define kVersionBuild_Coding [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]

系统信息

代码语言:javascript复制
//检查系统版本  
#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)  
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)  
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)  
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)  
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

设备信息

代码语言:javascript复制
//判断是真机还是模拟器  
#if TARGET_OS_IPHONE  
//iPhone真机  
#endif  
  
#if TARGET_IPHONE_SIMULATOR  
//iPhone模拟器  
#endif
代码语言:javascript复制
//-------------------获取设备大小-------------------------  
//NavBar高度  
#define NavigationBar_HEIGHT 44  
//获取屏幕 宽度、高度  
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)  
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
代码语言:javascript复制
#define ZJKeyWindow [UIApplication sharedApplication].keyWindow
#define ZJDevice_Is_iPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
#define ZJDevice_Is_iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define ZJDevice_Is_iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
#define ZJDevice_Is_iPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
#define ZJScreen_Bounds [UIScreen mainScreen].bounds
#define ZJScreen_Height [UIScreen mainScreen].bounds.size.height
#define ZJScreen_Width [UIScreen mainScreen].bounds.size.width

颜色

代码语言:javascript复制
//----------------------颜色类---------------------------  
// rgb颜色转换(16进制->10进制)  
#define ZJColorFromRGB(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]  
  
//带有RGBA的颜色设置  
#define ZJCOLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]  
  
// 获取RGB颜色  
#define ZJRGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]  
#define ZJRGB(r,g,b) ZJRGBA(r,g,b,1.0f)

弹窗

代码语言:javascript复制
#define kTipAlert(_S_, ...)     [[[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:(_S_), ##__VA_ARGS__] delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil] show]

接口

代码语言:javascript复制
//常用引用 与 接口地址
#import "AFNetworking/AFNetworking.h"

#define ZJAPI_BASE  @"http://www.psvmc.cn/"
#define ZJAPI_USERLOGIN  ZJAPI_BASE@"login"

变量

代码语言:javascript复制
#define ZJWeak(var, weakVar) __weak __typeof(&*var) weakVar = var
#define ZJWeak_(var) ZJWeak(var, weak_##var);
/** defines a weak `self` named `__weakSelf` */
#define ZJWeakSelf ZJWeak(self, __weakSelf);

#define ZJStrong(weakVar, _var) __strong __typeof(&*weakVar) _var = weakVar; if (!_var) return;
#define ZJStrong_(var) ZJStrong(weak_##var, _##var);
/** defines a strong `self` named `_self` from `__weakSelf` */
#define ZJStrongSelf ZJStrong(__weakSelf, _self);

QUEUE

代码语言:javascript复制
#define ZJ_QUEUE_GLOBAL dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define ZJ_QUEUE_MAIN dispatch_get_main_queue()

使用

代码语言:javascript复制
dispatch_async(ZJ_QUEUE_GLOBAL, ^{
    //其他线程操作
    dispatch_async(ZJ_QUEUE_MAIN, ^{
        //UI线程操作
    });
});

0 人点赞