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)
#define CHScreen_Bounds [UIScreen mainScreen].bounds
#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)
#define CHNavAndStaH (CHStatusH + (GC_Mac ? 78 : (GC_iPhone ? 44 : 50)))
#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]
#define CHViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]
#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];
#pragma mark - 快速实现单例设计模式
#define SingletonH(Instance) + (instancetype)share##Instance;- (void)resetInstance;
#if __has_feature(objc_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
#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
#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];
#define NSStringFormat(format, ...) [NSString stringWithFormat:format, ##__VA_ARGS__]
3、可能会产生的错误
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术