iOS开发常用宏定义

 

//获取屏幕 宽度、高度
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

 

// 根据版本号导入文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max #import <UserNotifications/UserNotifications.h> #endif

  

/**
 *  版本号
 *
 *  @param  CFBundleShortVersionString
 *
 *  @return kVersion
 */
#define kVersion [NSString stringWithFormat:@"%@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]]

 

/**
 *  iPhone or iPad
 */
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_PAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

  

#define IPHONE4S ((SCREEN.height == 480.0f)?YES:NO)
#define IPHONE5 ((SCREEN.height == 568.0f)?YES:NO)
#define IPHONE6 ((SCREEN.height == 667.0f)?YES:NO)
#define IPHONE6P ((SCREEN.height == 736.0f)?YES:NO)
// 操作系统宏
#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 dispatch_main_async_withsafe(block)\
if ([NSThread isMainThread]) {\
block();\
} else {\
dispatch_async(dispatch_get_main_queue(), block);\
}

#define dispatch_global_async_withsafe(block)\
if ([NSThread isMainThread]) {\
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),block);\
} else {\
block();\
}

 

单粒模式宏定义

#pragma mark - 实例定义
#define SingletenInterface \
+ (instancetype)sharedInstance;\


#pragma mark - 定义单例代码块
#define SingletenImpl \
static id _instace; \
\
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instace = [super allocWithZone:zone]; \
    }); \
    return _instace; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
    return _instace; \
} \
\
+ (instancetype)sharedInstance \
{ \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        _instace = [[self alloc] init]; \
    }); \
    return _instace; \
} \

 宏中使用##,传入参数作为shared方法名称:

//
//  Singleten.h
//  可变参数测试
//
//  Created by HJiang on 2017/9/1.
//  Copyright © 2017年 HJiang. All rights reserved.
//

#ifndef Singleten_h
#define Singleten_h

#pragma mark - 实例定义
#define SingletenInterface(name) \
+ (instancetype)shared##name;\


#pragma mark - 定义单例代码块
#define SingletenImpl(name) \
static id _instace; \
\
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
return _instace; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instace; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [[self alloc] init]; \
}); \
return _instace; \
} \
\


#endif /* Singleten_h */

 

定义开发模式和发布模式条件宏

#ifdef DEBUG
#define CCDebugLog(fmt,...) \
if ([[NSThread currentThread] isMainThread]) {\
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{\
NSString *newFrt = [@"[文件名:%s]\n [函数名:%s]\n [行号:%d]\n" stringByAppendingString:fmt ];\
NSString *logContent = [NSString stringWithFormat:newFrt,__FILE__, __FUNCTION__, __LINE__,##__VA_ARGS__];\
logContent = [NSString stringWithFormat:@"%@%@%@",@"#####",logContent,@"#####"];\
NSLog(@"%@",logContent);\
});\
}else{\
NSString *newFrt = [@"[文件名:%s]\n [函数名:%s]\n [行号:%d]\n" stringByAppendingString:fmt ];\
NSString *logContent = [NSString stringWithFormat:newFrt,__FILE__, __FUNCTION__, __LINE__,##__VA_ARGS__];\
logContent = [NSString stringWithFormat:@"%@%@%@",@"#####",logContent,@"#####"];\
NSLog(@"%@",logContent);\
}\

#else
#define CCDebugLog(fmt,...)
# endif

 

替换系统NSLog

#ifdef DEBUG
#define NSLog(format, ...) printf("\n[%s] %s [第%d行] %s\n", __TIME__, __FUNCTION__, __LINE__, [[NSString stringWithFormat:format, ## __VA_ARGS__] UTF8String]);
#else
#define NSLog(format, ...)
#endif

 

转载于:https://www.cnblogs.com/HJiang/p/7462230.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值