美文网首页程序员iOS DeveloperiOS 开发
《Effective Objective-C 2.0》读书笔记(

《Effective Objective-C 2.0》读书笔记(

作者: 栗子烤肉 | 来源:发表于2016-04-09 19:20 被阅读222次

    1.了解Objective-C语言的起源

    堆栈区别 其他
    • 深拷贝浅拷贝
      • 浅拷贝,只是对指针的拷贝,拷贝后两个指针指向同一个内存空间
      • 深拷贝不但对指针进行拷贝,而且对指针指向的内容进行拷贝,经深拷贝后的指针是指向两个不同地址的指针

    2.在类的头文件中尽量少引入其他头文件

    • #include 容易导致死循环

    • 除非确有必要,否则不要使用#import

      • 产生相互依赖问题
      • 增加编译时间
    • 尽量使用@class(forward declaring)

    3.多用字面量语法,少用与之等价的方法

    • 字面量语法其实就是语法糖
    • 更简洁、易于理解
    • 当数组、字典中有nil,会抛出异常

    <pre><code>
    -(void)example3_syntacticSugarWithNil{

    id object1 = @"object1";
    
    id object2 = nil;
    
    id object3 = @1;
    
    NSArray *arrayWithoutSyntacticSugar = [NSArray arrayWithObjects:object1,object2,object3, nil];
    
    pr_obj(arrayWithoutSyntacticSugar);
    
    NSArray *arrayWithSynacticSugar = @[object1,object2,object3];
    
    pr_obj(arrayWithSynacticSugar);
    

    }
    </pre></code>
    因为arrayWithObjects:方法依次处理各个参数,直到发现nil为止,因为object2是nil,所以方法会提前结束,arrayWithoutSyntacticSugar只包含object1


    结果

    使用字面量语法,当有nil时,会抛出异常,应用终止执行,便于发现错误。

    但对于很多app来说,app闪退并不是一件好事,用户体验不好。为了降低闪退率,而不使用字面量语法。表面上app的闪退率下降了,但实际上,为app埋了隐藏的bug,排查bug难度加大。在我看来,宁愿抛出异常出现闪退,也好过掩盖错误,为app埋下炸弹,某个时间突然爆炸,一发不可收拾。

    • 局限性

      • 如果自定义子类,无法用字面量语法创建对象
      • 使用字面量语法创建出来的字符串、数组、字典对象均为不可变

    编译成功:


    警告

    运行后,闪退:


    结果

    4.多用类型常量,少用#define预处理指令

    • #define
      • 没有类型信息
      • 重新定义常量值,编译器不会产生警告

    <pre><code>#define ANIMATION_DURATION 0.3</pre></code>

    • static const
      • 描述了常量的含义
      • 如果不加static,编译器默认添加extern
      • 如果修改const修饰符声明的变量,编译器会报错

    <pre><code>static const NSTimeInterval kAnimationDuration = 0.3;</pre></code>

    • extern
      • 在头文件使用extern声明全局常量,在相关实现文件中定义值

    <pre><code>extern NSString *const EOCLoginManagerDidLoginNotification;</pre></code>

    5.用枚举表示状态、选项、状态码

    • 枚举默认从0开始

    TestConnectionState定义:

    <pre><code>
    typedef enum {

    TestConnectionStateDisconnected,
    
    TestConnectionStateConnecting,
    
    TestConnectionStateConnected,
    

    }TestConnectionState;
    </pre></code>
    打印枚举值:
    <pre><code>
    -(void)example4_enumWithDefault{

    pr_int(TestConnectionStateDisconnected);
    
    pr_int(TestConnectionStateConnecting);
    
    pr_int(TestConnectionStateConnected);
    

    }
    </pre></code>

    结果
    • 枚举也可以指定数值

    <pre><code>
    typedef enum {

    TestConnectionStateStartWithTwoDisconnected = 2,
    
    TestConnectionStateStartWithTwoConnecting = 4,
    
    TestConnectionStateStartWithTwoConnected = 5,
    

    }TestConnectionStateStartWithTwo;
    </pre></code>
    打印枚举值:
    <pre><code>
    -(void)example4_enumStartWithTwo{

    pr_int(TestConnectionStateStartWithTwoDisconnected);
    
    pr_int(TestConnectionStateStartWithTwoConnecting);
    
    pr_int(TestConnectionStateStartWithTwoConnected);
    

    }
    </pre></code>


    结果
    • 如果把传递给某个方法的选项表示为枚举类型,而多个选项又可同时使用,那么将各个选项值定义为2的幂,通过按位或操作将其组合起来

    以UIViewAutoresizing为例,在UIView.h文件中可以看到UIViewAutoresizing的声明:
    <pre><code>
    typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {

    UIViewAutoresizingNone                 = 0,
    
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
    

    };
    </pre></code>
    通过按位与,可以快速判断是否包含某个选项:
    <pre><code>
    -(void)example4_UIViewAutoresizing{

    UIViewAutoresizing resizing = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
    
    if(resizing & UIViewAutoresizingFlexibleLeftMargin){
    
        pr_obj(@"UIViewAutoresizingFlexibleLeftMargin");
    
    }
    
    if(resizing & UIViewAutoresizingFlexibleHeight){
    
        pr_obj(@"UIViewAutoresizingFlexibleHeight");
    
    }
    

    }
    </pre></code>

    结果
    • 用NS_ENUM与NS_OPTIONS宏来定义枚举类型,并指明其底层数据类型,而不采用编译器所选的类型
    • (!!!!并没有任何提示)在处理枚举类型的switch语句不要实现default分支。加入新枚举后,编译器会提示switch语句并未处理所有枚举

    <pre><code>
    typedef NS_ENUM(NSInteger,TestSwitchState) {

    TestSwitchStateAAA,
    
    TestSwitchStateBBB,
    
    TestSwitchStateCCC,
    
    TestSwitchStateNone,
    
    TestSwitchStateDDD
    

    };
    </pre></code>
    结果并没有任何提示:


    结果

    相关文章

      网友评论

        本文标题:《Effective Objective-C 2.0》读书笔记(

        本文链接:https://www.haomeiwen.com/subject/qkkblttx.html