1. 判斷當(dāng)前環(huán)境是ARC還是MRC
#if __has_feature(objc_arc)
// 當(dāng)前的編譯器環(huán)境是ARC
#else
// 當(dāng)前的編譯器環(huán)境是MRC
#endif
2. 讀取info.plist文件中的數(shù)據(jù)
方式一
// File:獲取文件的全路徑 => 文件在哪(主bundle)
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Info.plist" ofType:nil];
// 1.解析info,plist
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
// 獲取當(dāng)前的版本號
NSString *Verision = dict[@"CFBundleShortVersionString"];
上述的方式比較麻煩,系統(tǒng)自已為我們提供了一個(gè)方法:
// 第二種方式獲取info.plist信息
NSString * Verision = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
3. PCH文件
PCH文件(Precompile Prefix Header File)疙渣,也就是預(yù)編譯頭文件高蜂,其作用就是尝盼,方便你一次性導(dǎo)入在多個(gè)文件中同時(shí)用到的頭文件步咪、宏或者URL地址等(全局使用)前普,可以有效的幫你節(jié)約時(shí)間,提高開發(fā)效率。但是离福,自從Xcode 6之后,這個(gè)文件默認(rèn)就不再提供了炼蛤,因?yàn)镻CH文件需要提前編譯,比較耗費(fèi)時(shí)間. 當(dāng)然如果你還想繼續(xù)使用的話妖爷,需要手動創(chuàng)建并配置。
使用注意點(diǎn):
- pch需要提前編譯
- 需要做一些判斷,判斷下當(dāng)前有沒有c文件,如果有c,就不導(dǎo)入OC的語法
//判斷是否是OC環(huán)境
#ifdef __OBJC__
// 1.存放一些公用的宏
#define Height (10)
// 2.存放一些公用的頭文件
#import "UIImage+Image.h"
// 3.自定義Log(輸出日志) 因?yàn)镹SLog是非常耗費(fèi)性能的
#ifdef DEBUG // 調(diào)試
#define XMGLog(...) NSLog(__VA_ARGS__)
#else // 發(fā)布
#define XMGLog(...)
#endif
#endif
4. 遍歷打印所有子視圖
// 獲取子視圖
- (void)getSub:(UIView *)view andLevel:(int)level {
NSArray *subviews = [view subviews];
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
NSString *blank = @"";
for (int i = 1; i < level; i++) {
blank = [NSString stringWithFormat:@" %@", blank];
}
NSLog(@"%@%d: %@", blank, level, subview.class);
[self getSub:subview andLevel:(level+1)];
}
}
比如我們想獲取self.navigationController.navigationBar
的子視圖結(jié)構(gòu)我們可以
[self getSub:self.navigationController.navigationBar andLevel:1];
打印結(jié)果:
單利模式
單例模式的作用 :可以保證在程序運(yùn)行過程中理朋,一個(gè)類只有一個(gè)實(shí)例赠涮,而且該實(shí)例易于供外界訪問子寓,從而方便地控制了實(shí)例個(gè)數(shù),并節(jié)約系統(tǒng)資源笋除。
利用GCD實(shí)現(xiàn)單利模式
//.h文件
#import <Foundation/Foundation.h>
@interface Singleton : NSObject
//單例方法
+(instancetype)sharedSingleton;
@end
//.m文件
#import "Singleton.h"
@implementation Singleton
//全局變量
static id _instance = nil;
//類方法斜友,返回一個(gè)單例對象
+(instancetype)sharedSingleton{
return [[self alloc] init];
}
//alloc內(nèi)部其實(shí)就是用allocWithZone:
+(instancetype)allocWithZone:(struct _NSZone *)zone{
//只進(jìn)行一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
return _instance;
}
//初始化方法
- (instancetype)init{
// 只進(jìn)行一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super init];
});
return _instance;
}
//copy在底層 會調(diào)用copyWithZone:
- (id)copyWithZone:(NSZone *)zone{
return _instance;
}
+ (id)copyWithZone:(struct _NSZone *)zone{
return _instance;
}
+ (id)mutableCopyWithZone:(struct _NSZone *)zone{
return _instance;
}
- (id)mutableCopyWithZone:(NSZone *)zone{
return _instance;
}
@end