AFNetworking源碼記

AFNNetworking版本 3.1

Day1 - 115Line

宏NS_DESIGNATED_INITIALIZER

  • 標(biāo)記所在方法為Designated構(gòu)造方法
  • 其他構(gòu)造方法必須最終調(diào)用此方法
  • 此方法必須調(diào)用父類的Designated構(gòu)造方法

關(guān)鍵字nullable / nonnull / _Nullable / _Nonnull

  • nullable/nonnull 用于屬性聲明, 返回類型, 參數(shù)類型, 在具體類型之前
  • _Nullable/_Nonnull用于單獨(dú)的變量申明, 在具體類型后面

通常默認(rèn)nononull行為, 則使用NS_ASSUME_NONNULL_BEGIN和NS_ASSUME_NONNULL_END宏進(jìn)行整體修飾.

Block的參數(shù)申明

(returnType/void (?^)(type name, type3 name2...)) block

宏FOUNDATION_EXPORT的使用

  • 用于通知名的全局申明
  • 在64位架構(gòu)中表示extern關(guān)鍵字

Day2 - 215Line

正確使用KVO添加和移除監(jiān)聽的屬性

NSStringFromSelector利用屬性的get方法獲得需要監(jiān)聽的屬性名,避免手動(dòng)輸入屬性名字符串錯(cuò)誤問題.

[task addObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesExpectedToReceive)) options:NSKeyValueObservingOptionNew context:NULL];

[task removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesExpectedToReceive))];    

Block 的typedef申明

typedef returnType (^BlockName)(type name, type2 name2,..)

typedef void (^AFURLSessionTaskCompletionHandler)(NSURLResponse *response, id responseObject, NSError *error);

解決Block中的強(qiáng)引用

在持有block內(nèi)部對(duì)外邊的已經(jīng)持有的屬性變量進(jìn)行使用時(shí),block會(huì)捕獲該變量,也會(huì)強(qiáng)引用它,形成循環(huán)引用,想要避免就要先在外部進(jìn)行weak引用該變量,然后在block內(nèi)進(jìn)行strong引用weak變量.

__weak __typeof__(task) weakTask = task;    
[self.uploadProgress setCancellationHandler:^{
    __typeof__(weakTask) strongTask = weakTask;
    [strongTask cancel];
}];

Day3 -

利用Objective-C消息的動(dòng)態(tài)轉(zhuǎn)發(fā)實(shí)現(xiàn)MethodSwizzle

  • 需要引入C庫<objc/runtime.h>
  • 若是Objective-C版本則在load方法進(jìn)行Method Swizzle操作,若是Swift在initialize方法中進(jìn)行.
  • 方法實(shí)現(xiàn)對(duì)象Method利用class_getInstanceMethod / class_getClassMethod獲得,先使用class_addMethod()將自定義的方法添加到當(dāng)前類,返回bool表示是否添加成功.在成功添加的前提下進(jìn)行交換原方法實(shí)現(xiàn)的交換method_exchangeImplementations.
static inline void af_swizzleSelector(Class theClass, SEL originalSelector, SEL swizzledSelector) {
    Method originalMethod = class_getInstanceMethod(theClass, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(theClass, swizzledSelector);
    method_exchangeImplementations(originalMethod, swizzledMethod);
}

static inline BOOL af_addMethod(Class theClass, SEL selector, Method method) {
    return class_addMethod(theClass, selector,  method_getImplementation(method),  method_getTypeEncoding(method));
}

+ (void)swizzleResumeAndSuspendMethodForClass:(Class)theClass {
    Method afResumeMethod = class_getInstanceMethod(self, @selector(af_resume));
    Method afSuspendMethod = class_getInstanceMethod(self, @selector(af_suspend));

    if (af_addMethod(theClass, @selector(af_resume), afResumeMethod)) {
        af_swizzleSelector(theClass, @selector(resume), @selector(af_resume));
    }

    if (af_addMethod(theClass, @selector(af_suspend), afSuspendMethod)) {
        af_swizzleSelector(theClass, @selector(suspend), @selector(af_suspend));
    }
}

斷言NSAssert的使用

  • 若條件Bool為假,表示斷言失敗,程序終止
  • Assert系列函數(shù)只在開發(fā)環(huán)境中有效,在發(fā)布環(huán)境中會(huì)被編譯器忽略
  • 盡可能使用Assert函數(shù)提高程序的安全性
- (void)af_resume {
    NSAssert([self respondsToSelector:@selector(state)], @"Does not respond to state");
    NSURLSessionTaskState state = [self state];
    [self af_resume];
    
    if (state != NSURLSessionTaskStateRunning) {
        [[NSNotificationCenter defaultCenter] postNotificationName:AFNSURLSessionTaskDidResumeNotification object:self];
    }
}

使用關(guān)鍵字__block修飾變量的目的

Block和__block變量都是結(jié)構(gòu)體變量, __block拓展

  • 讓修飾后的外部的局部變量能在Block中被修改
  • 能在Block中控制修飾后的變量的生命周期,避免循環(huán)引用
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容