程序员iOS DeveloperiOS 开发

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

2016-04-09  本文已影响222人  栗子烤肉

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

堆栈区别 其他

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

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

<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预处理指令

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

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

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

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

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>


结果

以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>

结果

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

TestSwitchStateAAA,

TestSwitchStateBBB,

TestSwitchStateCCC,

TestSwitchStateNone,

TestSwitchStateDDD

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


结果
上一篇 下一篇

猜你喜欢

热点阅读