Apple开发_pch文件的创建与配置

1、ios中pch文件的创建与配置

2、宏定义

  • 这里放的主要是开发中常用的宏定义。
  • 2.1 设备信息

// 判断设备型号
#define GC_iPad      ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
#define GC_iPhone    ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
#define GC_Mac       !(GC_iPad && GC_iPhone)

/** 设备尺寸 ==> {{0, 0}, {414, 896}} , 格式化打印使用 NSStringFromCGRect(CHScreen_Bounds)*/
#define CHScreen_Bounds            [UIScreen mainScreen].bounds
/** 设备尺寸 ==> {414, 896} , 格式化打印使用 NSStringFromCGSize(CHScreen_Size)*/
#define CHScreen_Size              CHScreen_Bounds.size
/** 设备屏幕高(点)*/
#define CHScreenH                  CHScreen_Size.height
/** 设备屏幕宽(点) */
#define CHScreenW                  CHScreen_Size.width
/** 手机屏幕宽 */
#define GC_iPhoneW                 GC_iPhone ? CHScreenW : 414

// 状态栏高度
#define CHStatusH [UIApplication sharedApplication].statusBarFrame.size.height
// 异型全面屏(刘海屏)
#define FullScreen (CHStatusH > 20)
// 导航栏高度:Mac78(78是自定的) 手机44 平板50 + 状态栏高度
#define CHNavAndStaH (CHStatusH + (GC_Mac ? 78 : (GC_iPhone ? 44 : 50)))
// tabbar高度
#define CHTabbarH (FullScreen ? 83 : 49)
// 顶部的安全距离
#define CHTabbar_SafeTopMargin (CHStatusH - 20)
// 底部的安全距离
#define CHTabbar_SafeBottomMargin (CHTabbarH - 49)
// 导航条与状态栏高度
#define CHNavigationH          44

#define Cur_Window  [[[UIApplication sharedApplication] delegate] window]
  • 2.2 UI控件

/** 设置view圆角和边框 */
#define CHViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]

/** 设置view边框 */
#define CHViewBorder(View, Width, Color)\
\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]
#define NotificationCenter                                     [NSNotificationCenter defaultCenter]
// 直接发送消息,不带消息内容
#define NSNotificationCenterPostName(notificationName)         [NotificationCenter postNotificationName:notificationName object:self];
// 移除所有的观察者
#define NSNotificationCenterRemoveObserver                     [NotificationCenter removeObserver:self];
// 移除所有的观察者
#define NSNotificationCenterRemoveObserverr(notificationName)  [NotificationCenter removeObserver:self name:notificationName object:nil];
// 添加通知中无参数的观察者
#define NSNotificationCenterAddObserverSelfDo(method, notificationName)  [NotificationCenter addObserver:self selector:@selector(method) name:notificationName object:nil];
// 发送消息,带消息内容
#define NSNotificationCenterPostUserInfo(notificationName, message)      [NotificationCenter postNotificationName:notificationName object:message userInfo:message];
/*
 使用示例:
 GCTool.h文件
 
 #import <Foundation/Foundation.h>

 @interface GCTool : NSObject

 SingletonH(Instance)
 
 @end
 
 GCTool.m文件
 
 #import "GCTool.h"
 
 @implementation GCTool
 
 SingletonM(Instance)
 
 @end

// 宏定义简化
#define GC_Tool  [GCTool shareInstance]
// 有时候需要重置单例
[GC_Tool resetInstance];
 */
#pragma mark - 快速实现单例设计模式

/** 单例模式 .h文件的实现:包含单粒常规工厂方法 shareInstance 与 重置单粒方法 resetInstance*/
#define SingletonH(Instance) + (instancetype)share##Instance;- (void)resetInstance;

/** 单例模式 .m文件的实现:包含单粒常规工厂方法 shareInstance 与 重置单粒方法 resetInstance */
// 注意⚠️:暂时不支持MRC情况下的 重置单粒方法 resetInstance
#if __has_feature(objc_arc) // 是ARC
#define SingletonM(Instance) \
static id _gc_##Instance = nil; \
static dispatch_once_t gc_Token##Instance; \
- (id)init \
{ \
dispatch_once(&gc_Token##Instance, ^{ \
_gc_##Instance = [super init]; \
}); \
return _gc_##Instance; \
} \
\
+ (instancetype)share##Instance \
{ \
return [[self alloc] init]; \
}\
- (void)resetInstance {\
    gc_Token##Instance = 0;\
    _gc_##Instance = nil;\
}
#else // 不是ARC
#define SingletonM(Instance); \
static id _gc_##Instance = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
if (_gc_##Instance == nil) { \
static dispatch_once_t gc_Token##Instance; \
dispatch_once(&gc_Token##Instance, ^{ \
_gc_##Instance = [super allocWithZone:zone]; \
}); \
} \
return _gc_##Instance; \
} \
\
- (id)init \
{ \
static dispatch_once_t gc_Token##Instance; \
dispatch_once(&gc_Token##Instance, ^{ \
_gc_##Instance = [super init]; \
}); \
return _gc_##Instance; \
} \
\
+ (instancetype)share##Instance \
{ \
return [[self alloc] init]; \
} \
\
- (oneway void)release \
{ \
\
} \
\
- (id)retain \
{ \
return self; \
} \
\
- (NSUInteger)retainCount \
{ \
return 1; \
} \
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _gc_##Instance; \
} \
\
+ (id)mutableCopyWithZone:(struct _NSZone *)zone \
{ \
return _gc_##Instance; \
}

#endif
// 检查是否真机
#if TARGET_IPHONE_SIMULATOR // 模拟器

#elif TARGET_OS_IPHONE // 真机

#endif
// 01.使用这个方法不会忽略系统的、第三方库的打印,使用这个方法可以打印属于自己的日志
#define CHLog(...) printf("第%d行: %s\n", /* 行号 */__LINE__, /* 动态参数 */[[NSString stringWithFormat:__VA_ARGS__] UTF8String]);
// 本地化
#define MyLocal(simplifiedChinese) NSLocalizedString(simplifiedChinese, nil)
#define SDRUserDefaults                                 [NSUserDefaults standardUserDefaults]
/** 存储对象 */
#define NSUserDefaultsSetObjForKey(obj, key)            [SDRUserDefaults setObject:obj forKey:key];[SDRUserDefaults synchronize];
#define NSUserDefaultsTakeObj(key)                      [SDRUserDefaults objectForKey:key]
/** 整数存储 */
#define NSUserDefaultsSetIntForKey(integer, key)        [SDRUserDefaults setInteger:integer forKey:key];[SDRUserDefaults synchronize];
#define NSUserDefaultsTakeInt(key)                      [SDRUserDefaults integerForKey:key]
/** 浮点存储 */
#define NSUserDefaultsSetFloatForKey(float, key)        [SDRUserDefaults setFloat:float forKey:key];[SDRUserDefaults synchronize];
#define NSUserDefaultsTakeFloat(key)                    [SDRUserDefaults floatForKey:key]
/** 布尔值存储 */
#define NSUserDefaultsSetBoolForKey(boolValue, key)     [SDRUserDefaults setBool:boolValue forKey:key];[SDRUserDefaults synchronize];
#define NSUserDefaultsTakeBool(key)                     [SDRUserDefaults boolForKey:key]

// 移除存储
#define NSUserDefaultsRemoveObject(key)                 [SDRUserDefaults removeObjectForKey:key];[SDRUserDefaults synchronize];
  • 2.9 其他

/** 动态的字符串格式化宏 */
#define NSStringFormat(format, ...)     [NSString stringWithFormat:format, ##__VA_ARGS__]

3、可能会产生的错误

posted @ 2018-07-30 21:58  CH520  阅读(350)  评论(0编辑  收藏  举报