Day1 - 115Line
宏NS_DESIGNATED_INITIALIZER
- 標記所在方法為Designated構造方法
- 其他構造方法必須最終調用此方法
- 此方法必須調用父類的Designated構造方法
關鍵字nullable / nonnull / _Nullable / _Nonnull
- nullable/nonnull 用于屬性聲明, 返回類型, 參數類型, 在具體類型之前
- _Nullable/_Nonnull用于單獨的變量申明, 在具體類型后面
通常默認nononull行為, 則使用NS_ASSUME_NONNULL_BEGIN和NS_ASSUME_NONNULL_END宏進行整體修飾.
Block的參數申明
(returnType/void (?^)(type name, type3 name2...)) block
宏FOUNDATION_EXPORT的使用
- 用于通知名的全局申明
- 在64位架構中表示extern關鍵字
Day2 - 215Line
正確使用KVO添加和移除監聽的屬性
NSStringFromSelector利用屬性的get方法獲得需要監聽的屬性名,避免手動輸入屬性名字符串錯誤問題.
[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中的強引用
在持有block內部對外邊的已經持有的屬性變量進行使用時,block會捕獲該變量,也會強引用它,形成循環引用,想要避免就要先在外部進行weak引用該變量,然后在block內進行strong引用weak變量.
__weak __typeof__(task) weakTask = task;
[self.uploadProgress setCancellationHandler:^{
__typeof__(weakTask) strongTask = weakTask;
[strongTask cancel];
}];
Day3 -
利用Objective-C消息的動態轉發實現MethodSwizzle
- 需要引入C庫
<objc/runtime.h>
- 若是Objective-C版本則在
load
方法進行Method Swizzle操作,若是Swift在initialize
方法中進行. - 方法實現對象Method利用
class_getInstanceMethod / class_getClassMethod
獲得,先使用class_addMethod()
將自定義的方法添加到當前類,返回bool表示是否添加成功.在成功添加的前提下進行交換原方法實現的交換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系列函數只在開發環境中有效,在發布環境中會被編譯器忽略
- 盡可能使用Assert函數提高程序的安全性
- (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];
}
}
使用關鍵字__block修飾變量的目的
Block和__block變量都是結構體變量, __block拓展
- 讓修飾后的外部的局部變量能在Block中被修改
- 能在Block中控制修飾后的變量的生命周期,避免循環引用