一阵苇、Xcode添加PCH文件
①
在Supporting Files目錄下,選擇 File > New > File > iOS > Other > PCH File 然后點(diǎn)擊下一步;
②
PCH默認(rèn)名字 PrefixHeader.pch 然后創(chuàng)建(可以改別的名字):
可以在新建的PCH文件中寫代碼
③
找到 Project > Build Settings 搜索 “Prefix Header“感论;
④
輸入: 項(xiàng)目名字/PrefixHeader.pch (如 weihuan/PrefixHeader.pch )绅项;
⑤
將Precompile Prefix Header改為YES,預(yù)編譯后的pch文件會(huì)被緩存起來(lái)比肄,可以提高編譯速度趁怔;
⑥
Clean 并運(yùn)行你的項(xiàng)目。
二薪前、常用的宏
// 0.弱引用/強(qiáng)引用
#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.常用對(duì)象
#define kApplication [UIApplication sharedApplication]
#define kAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)
#define kUserDefaults [NSUserDefaults standardUserDefaults]
#define kNotificationCenter [NSNotificationCenter defaultCenter]
// 4.獲取常用
//APP版本號(hào)
#define kAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
//系統(tǒng)版本號(hào)
#define kSystemVersion [[UIDevice currentDevice] systemVersion]
//獲取當(dāng)前語(yǔ)言
#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.判斷系統(tǒng)版本
#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.判斷手機(jī)型號(hào)
#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.測(cè)試的時(shí)候打印語(yǔ)句,發(fā)布程序的時(shí)候自動(dòng)去除打印語(yǔ)句
#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>
共
同
進(jìn)
步
給個(gè)喜歡喔O(∩_∩)O