Effective Objective-C 2.0 读书笔记

title: "Effective Objective-C 2.0 读书笔记"
date: 2015-09-04 14:23:39
tags: "读书"
categories: "读书"


  1. oc 是有smalltall 演化而来,动态绑定

  2. 理解 c 内存模型

    对象都在堆上,手动管理

  3. 在类的头文件中尽量少的引入其他头文件(@class)
    除非确有必要,否则不要引入头文件。一般来说,应在某个类的头文件中使用向前声明来提及别的类,并在实现文件中引入那些类的头文件。这样做可以尽量降低类之间的耦合(coupling).
    有时无法使用向前声明,比如要声明某个类遵循一项协议。这种情况下,应尽量把“该类遵循某协议”的这条声明移至“class-continuation分类中”。如果不行的话,就把协议单独放在一个头文件中,然后将其引入。
    3.多使用字面量常量 ,字面量常量对象都是不可变得,使用可使用mutablecopy

  4. 多用类型常量(带类型信息),少用预处理命令

     #define ANIAMTION_DURATION  0.3   
     static const NSTimeInterval kAnimationDuration  = 0.3   
     (只在编译单元可见)   
     extern NSString * const EOCStringConstant;  (公开)
     在实现文件中  NSString * const EOCStringConstant = @“hello”; 
    
  5. 用枚举表示状态,选项和状态码
    枚举和switch 使用时不需要default 分支

  6. 理解属性的概念

    @dynamic 编译器不会为这个属性自动生成存取方法或实例变量(_firstName)

    readonly 若某属性仅可于对象内部修改,则在“class-continuation分类”中将其由readonly属性扩展为readwrite属性
    当我们声明一个 readonly 的属性,外部可能会通过 KVC 修改该属性值。
    为了避免 KVC 修改属性值,须将定义属性所在类的类方法 + (BOOL)accessInstanceVariablesDirectly 重写,使其返回 NO.

    assign 基本数据类型
    strong 拥有关系 保留新值,释放旧值,并把新值设置上去
    weak 非拥有关系,既不保留新值,也不释放旧值(同assign),当所指对象销毁时,自动至为nil
    copy 同strong 类似,然后并不会保留新值,而是copy 一份

  7. 在对象内部尽量直接访问实例变量、

    • 直接访问不会触发kvo,不会触发存取方法
    • 懒加载的时候要使用属性访问
    • dealloc的时候使用直接访问
  8. 以“类族模式”隐藏实现细节

    • 系统框架中经常使用类族
    • 类族模式可以把实现细节隐藏在一套简单的公共接口后面
  9. 在既有类中使用关联对象存放自定义数据

     设置关联对象
     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);    
}
  • 可以通过关联对象把两个对象关联起来
  • 定义关联对象的时候可指定内存管理语义
  • 只有在其他办法不可行的时候才选用关联对象
  1. 用“方法调配技术”调试“黑盒方法” ( 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);
}   

在运行期可以向类中新增或者替换选择子所对应的方法实现

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容