title: "Effective Objective-C 2.0 讀書筆記"
date: 2015-09-04 14:23:39
tags: "讀書"
categories: "讀書"
oc 是有smalltall 演化而來,動態綁定
-
理解 c 內存模型
對象都在堆上,手動管理
在類的頭文件中盡量少的引入其他頭文件(@class)
除非確有必要,否則不要引入頭文件。一般來說,應在某個類的頭文件中使用向前聲明來提及別的類,并在實現文件中引入那些類的頭文件。這樣做可以盡量降低類之間的耦合(coupling).
有時無法使用向前聲明,比如要聲明某個類遵循一項協議。這種情況下,應盡量把“該類遵循某協議”的這條聲明移至“class-continuation分類中”。如果不行的話,就把協議單獨放在一個頭文件中,然后將其引入。
3.多使用字面量常量 ,字面量常量對象都是不可變得,使用可使用mutablecopy-
多用類型常量(帶類型信息),少用預處理命令
#define ANIAMTION_DURATION 0.3 static const NSTimeInterval kAnimationDuration = 0.3 (只在編譯單元可見) extern NSString * const EOCStringConstant; (公開) 在實現文件中 NSString * const EOCStringConstant = @“hello”;
用枚舉表示狀態,選項和狀態碼
枚舉和switch 使用時不需要default 分支-
理解屬性的概念
@dynamic 編譯器不會為這個屬性自動生成存取方法或實例變量(_firstName)
readonly 若某屬性僅可于對象內部修改,則在“class-continuation分類”中將其由readonly屬性擴展為readwrite屬性
當我們聲明一個 readonly 的屬性,外部可能會通過 KVC 修改該屬性值。
為了避免 KVC 修改屬性值,須將定義屬性所在類的類方法 + (BOOL)accessInstanceVariablesDirectly 重寫,使其返回 NO.assign 基本數據類型
strong 擁有關系 保留新值,釋放舊值,并把新值設置上去
weak 非擁有關系,既不保留新值,也不釋放舊值(同assign),當所指對象銷毀時,自動至為nil
copy 同strong 類似,然后并不會保留新值,而是copy 一份 -
在對象內部盡量直接訪問實例變量、
- 直接訪問不會觸發kvo,不會觸發存取方法
- 懶加載的時候要使用屬性訪問
- dealloc的時候使用直接訪問
-
以“類族模式”隱藏實現細節
- 系統框架中經常使用類族
- 類族模式可以把實現細節隱藏在一套簡單的公共接口后面
-
在既有類中使用關聯對象存放自定義數據
設置關聯對象 void objc_setAssociatedObject (id object, void * key, id value, objc_associationPolicy policy) 獲取關聯對象的值 id objc_getAssociatedObejct(id obejct, void *key) 移除指定對象的全部關聯對象 void objc_removeAssociatedObejcts(id obejct)
舉個栗子
#import <objc/runtime.h>
static void *EOCMyAlertViewKey = @"EOCMyAlertViewKey";
- (void)askUserAQuestion {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Question"
message:@"What do you want to do?"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"繼續", nil];
void (^block)(NSInteger buttonIdx) = ^(NSInteger buttonIdx) {
if (buttonIdx == 0) {
// cancle
}else {
//sure
}
};
objc_setAssociatedObject(alertView, EOCMyAlertViewKey, block, OBJC_ASSOCIATION_COPY);
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
void (^block)(NSInteger) = objc_getAssociatedObject(alertView, EOCMyAlertViewKey);
block(buttonIndex);
}
- 可以通過關聯對象把兩個對象關聯起來
- 定義關聯對象的時候可指定內存管理語義
- 只有在其他辦法不可行的時候才選用關聯對象
-
用“方法調配技術”調試“黑盒方法” ( method swizzling )
舉個??
#import "UIViewController+Tracking.h"
#import <objc/runtime.h>
@implementation UIViewController (Tracking)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originSelector = @selector(viewWillAppear:);
SEL swizzledSelector = @selector(rh_viewWillAppear:);
Method originalMenthod = class_getInstanceMethod(class, originSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
//添加新方法
BOOL didAddMethod = class_addMethod(class, originSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
//如果已經添加,就方法替換
if (didAddMethod) {
//Replaces the implementation of a method for a given class.
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMenthod), method_getTypeEncoding(originalMenthod));
}else {
method_exchangeImplementations(originalMenthod, swizzledMethod);
}
});
}
- (void)rh_viewWillAppear:(BOOL)animated {
[self rh_viewWillAppear:animated];
NSLog(@"viewWillAppear: %@", self);
}
在運行期可以向類中新增或者替換選擇子所對應的方法實現