系統(tǒng)框架
第47條:熟悉系統(tǒng)框架
- 許多系統(tǒng)框架都可以直接使用痹屹,其中最重要的是Foundation與CoreFoundation,這兩個(gè)框架提供了構(gòu)建應(yīng)用程序所需的許多核心功能。
- 很多常見任務(wù)都能用框架來(lái)做,例如:視頻處理、網(wǎng)絡(luò)通信、數(shù)據(jù)管理。(
CFNetwork
,CoreAudio
,AVFoundation
,CoreData
,CoreText
都需要了解下) - 用純C寫成的框架與用Objective-C寫成的一樣重要,所以我們掌握C語(yǔ)言還是很重要的疫粥。·
第48條:多用塊枚舉腰懂,少用for循環(huán)
NSArray * testArray = @[@"1",@"2",@"3",@"4",@"5"];
// for 循環(huán)
for(int i = 0; i < testArray.count; i++)
{
NSLog(@"testString === %@",testArray[i]);
}
// 快速遍歷
for(NSString * testString in testArray)
{
NSLog(@"testString === %@",testString);
}
// 基于塊的遍歷模式
[testArray enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL * stop){
NSLog(@"testString === %@",testArray[idx]);
}];
推薦使用最后一種梗逮,NSDictionary 也可以使用它
NSDictionary * testDic = @{@"1":@"one",@"2":@"two"};
[testDic enumerateKeysAndObjectsUsingBlock:^(NSString * key,id object,BOOL * stop){
NSLog(@"test === %@",testDic[key]);
}];
總體來(lái)看,塊枚舉法擁有其他遍歷方式都具備的優(yōu)勢(shì)绣溜,而且還能帶來(lái)更多好處慷彤,在遍歷字典的時(shí)候,還可以同時(shí)提供鍵和值怖喻,還有選項(xiàng)可以開啟并發(fā)迭代功能底哗,所以多寫點(diǎn)代碼還是值的。
第49條:對(duì)自定義其內(nèi)存管理語(yǔ)義的collection使用無(wú)縫橋接
這個(gè)真心沒用過锚沸,不了解跋选,后期學(xué)習(xí)中···
大致看了下,略微了解了
通過無(wú)縫橋接技術(shù)哗蜈,可以在
Foundation
框架中的Objective-C
對(duì)象與CoreFoundation
框架中的C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之間來(lái)回轉(zhuǎn)換前标。
第50條:構(gòu)建緩存時(shí)選用NSCache而非NSDictionary
簡(jiǎn)單的說,NSCache是Foundation 框架專門來(lái)處理這種任務(wù)而設(shè)計(jì)的距潘。
第51條:精簡(jiǎn) initialize 與 load的實(shí)現(xiàn)代碼
這樣有助于我們程序的響應(yīng)能力炼列,也能減少引入“依賴“的幾率。
第52條:別忘了NSTime 會(huì)保留其目標(biāo)對(duì)象
這是由于計(jì)時(shí)器會(huì)保留其目標(biāo)對(duì)象音比,所以反復(fù)執(zhí)行任務(wù)通常會(huì)導(dǎo)致應(yīng)用程序出問題俭尖,也就是說很容易造成循環(huán)引用。
[NSTimer scheduledTimerWithTimeInterval:4.0f
target:self
selector:@selector(afterThreeSecondBeginAction)
userInfo:nil
repeats:YES];
也就是說一旦開啟了反復(fù)執(zhí)行的話,可以擴(kuò)充NSTime的功能稽犁,用block來(lái)打破循環(huán)引用焰望。
#import <Foundation/Foundation.h>
@interface NSTimer (YPQBlocksSupport)
+ (NSTimer *)ypq_scheduledTimeWithTimeInterval:(NSTimeInterval)interval
block:(void(^)())block
repeats:(BOOL)repeats;
@end
#import "NSTimer+YPQBlocksSupport.h"
@implementation NSTimer (YPQBlocksSupport)
+ (NSTimer *)ypq_scheduledTimeWithTimeInterval:(NSTimeInterval)interval
block:(void(^)())block
repeats:(BOOL)repeats
{
return [self scheduledTimerWithTimeInterval:interval
target:self
selector:@selector(ypq_blockInvoke:) userInfo:[block copy]
repeats:repeats];
}
- (void)ypq_blockInvoke:(NSTimer *)timer
{
void (^block)() = timer.userInfo;
if(block)
{
block();
}
}
@end
調(diào)用
__weak ViewController * weakSelf = self;
[NSTimer ypq_scheduledTimeWithTimeInterval:4.0f
block:^{
ViewController * strongSelf = weakSelf;
[strongSelf afterThreeSecondBeginAction];
}
repeats:YES];
上面這段代碼時(shí)這樣的,它先定義了一個(gè)弱引用缭付,令其指向self
柿估,然后使塊捕獲這個(gè)引用循未,而不直接去捕獲self
變量陷猫。也就是說,self
不會(huì)為計(jì)時(shí)器所保留的妖。當(dāng)開始執(zhí)行的時(shí)候绣檬,立刻生成strong
引用,以保證實(shí)例在執(zhí)行期間持續(xù)存活嫂粟。
而且我們一般在
dealloc
的時(shí)候娇未,不要忘了調(diào)用計(jì)時(shí)器中的invalidate
方法。
整體來(lái)說星虹,感覺這本書還是相當(dāng)不錯(cuò)零抬,很推薦。
而我個(gè)人來(lái)說宽涌,其中有好多條理解還不是很深刻平夜,后期需要積累來(lái)說,來(lái)再次更新卸亮,學(xué)習(xí)是不停止的忽妒,??。