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、可能会产生的错误
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
· 程序员转型AI:行业分析