iOS常用宏定义

定义一个宏

使用两个”##“来定义一个宏
#define LXWeakSelf(type)  __weak typeof(type) weak##type = type;
- (void)viewDidLoad {
    [super viewDidLoad];
    LXWeakSelf(self);
    [weakself test:@"liuxingxing"];
}

- (void)test: (NSString *)name {
    NSLog(@"%@",name);
}

##是连接的作用, 即当使用上面的宏会把weak与输入的type值连接起来.

使用一个”#“来定义一个宏

我们一般定义的宏是这样的:

#define LXLogA(str) [NSString stringWithFormat:@"%@",str]

LXLogA(@"你好呀");
NSLog(@"%@", LXLogA(@"你好呀"));

我们呢可以通过使用一个”#“来定义宏:

#define LXLogB(str) [NSString stringWithFormat:@"%@",@#str]

LXLogB(你好 世界 你好 中国);
NSLog(@"%@", LXLogB(你好 世界 你好 中国));

我们可以看到LXLogB使用时不再需要@””来包含字符串。也就是说,@#替换了@””。

iOS常用的宏总结

打印日志
#define NSLog(format, ...) do {                                               \
  fprintf(stderr, "<%s : %d> %s\n",                                           \
  [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String],  \
  __LINE__, __func__);                                                        \
  (NSLog)((format), ##__VA_ARGS__);                                           \
  fprintf(stderr, "-------\n");                                               \
} while (0)
//自定义NSLog
#ifdef DEBUG
#define LXLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define LXLog(...)
#endif
打印rect相关的信息
#define NSLogRect(rect) NSLog(@"%s x:%.4f, y:%.4f, w:%.4f, h:%.4f",     \
        #rect, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)

#define NSLogSize(size) NSLog(@"%s w:%.4f, h:%.4f", #size, size.width, size.height)

#define NSLogPoint(point) NSLog(@"%s x:%.4f, y:%.4f", #point, point.x, point.y)
字符串、数组、字典是否为空
#define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )

#define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)

#define kDictIsEmpty(dic) (dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys.count == 0)
获取屏幕的宽度、高度、大小
#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)
// 配合其他的宏来打印size的大小
NSLogSize(SCREEN_SIZE);
获取状态栏的高度
#define stautsBarHeight ([UIApplication sharedApplication].statusBarFrame.size.height)
获取APP的版本号和系统的版本号
#define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]

#define kSystemVersion [[UIDevice currentDevice] systemVersion]
获取当前语言
#define kCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
判断是否为iPhone和iPad
#define kIsiPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define kIsiPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
获取沙盒相关的路径
//获取temp
#define kPathTemp NSTemporaryDirectory()

//获取沙盒 Document
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]

//获取沙盒 Cache
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
判断是真机还是模拟器
#if TARGET_OS_IPHONE
//iPhone Device
#endif

#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif
颜色
//RGB格式
#define kRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]

//RGBA格式 (A:alpha)
#define kRGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]

//随机色生成
#define kRandomColor KRGBColor(arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0,arc4random_uniform(256)/255.0) 

//16进制形式
#define kColorWithHex(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]
GCD的宏定义
//GCD - 一次性执行
#define kDISPATCH_ONCE_BLOCK(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);
弧度和角度之间的转换
//由角度转换为弧度
#define kDegreesToRadian(x) (M_PI * (x) / 180.0)

//由弧度转换为角度
#define kRadianToDegrees(radian) (radian * 180.0) / (M_PI)

   转载规则


《iOS常用宏定义》 刘星星 采用 知识共享署名 4.0 国际许可协议 进行许可。
  目录