一、Xcode添加PCH文件
①
在Supporting Files目录下,选择 File > New > File > iOS > Other > PCH File 然后点击下一步;
②
PCH默认名字 PrefixHeader.pch 然后创建(可以改别的名字):
可以在新建的PCH文件中写代码
③
找到 Project > Build Settings 搜索 “Prefix Header“;
④
输入: 项目名字/PrefixHeader.pch (如 weihuan/PrefixHeader.pch );
⑤
将Precompile Prefix Header改为YES,预编译后的pch文件会被缓存起来,可以提高编译速度;
⑥
Clean 并运行你的项目。
二、常用的宏
// 0.弱引用/强引用
#define kWeakSelf(type) __weak typeof(type) weak##type = type;
#define kStrongSelf(type) __strong typeof(type) type = weak##type;
// 1.获得颜色
#define kHexColor(c) [UIColor colorWithRed:((float)((c & 0xFF0000) >> 16))/255.0 green:((float)((c & 0xFF00) >> 8))/255.0 blue:((float)(c & 0xFF))/255.0 alpha:1.0f]
#define kRGBA(r, g, b, a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define kRGB(r, g, b) RGBA(r, g, b, 1.0f)
#define kMainColor HEXCOLOR(0xadc64c)
//#define kMainColor RGB(133, 204, 167)
#define kBgColor HEXCOLOR(0xF0F0F0)
// 2.屏幕尺寸
#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height
#define kStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
#define kNavigationViewHeight (SCREEN_HEIGHT - 64)
#define kTabNavViewHeight (kNavigationViewHeight - 44)
// 3.常用对象
#define kApplication [UIApplication sharedApplication]
#define kAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)
#define kUserDefaults [NSUserDefaults standardUserDefaults]
#define kNotificationCenter [NSNotificationCenter defaultCenter]
// 4.获取常用
//APP版本号
#define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
//系统版本号
#define kSystemVersion [[UIDevice currentDevice] systemVersion]
//获取当前语言
#define kCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
//判断是否为iPhone
#define kIsiPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
//判断是否为iPad
#define kIsiPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
//判断是否为横屏
#define kIsLandscape ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight)
//获取沙盒Document路径
#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
//获取沙盒temp路径
#define kTempPath NSTemporaryDirectory()
//获取沙盒Cache路径
#define kCachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
// 5.判断系统版本
#define kiOS6OrLater ([[UIDevice currentDevice].systemVersion doubleValue] >= 6.0)
#define kiOS7OrLater ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0)
#define kiOS8OrLater ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0)
#define kiOS9OrLater ([[UIDevice currentDevice].systemVersion doubleValue] >= 9.0)
#define kiOS10OrLater ([[UIDevice currentDevice].systemVersion doubleValue] >= 10.0)
// 6.判断手机型号
#define kIsiPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
#define kIsiPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define kIsiPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
#define kIsiPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
// 7.测试的时候打印语句,发布程序的时候自动去除打印语句
#ifdef DEBUG
#define MyLog(...) NSLog(__VA_ARGS__)
#else
#define MyLog(...)
#endif
另外
定义常量 苹果推荐用const
//
static const NSIntegral kLimited = 10;
static NSString * const kName = @"userName";
//全局
//.h
extern NSString *const kName;
//.m
NSString *const kName = @"userName";
</br>
共
同
进
步
给个喜欢喔O(∩_∩)O