一、使用向前声明(@class)的好处

1、有效减少编译器编译的时间;

2、有效避免循环引用;

二、字面量语法的好处

1、字面常量

精简、可用于所有数据类型,如:

NSNumber *intNumber = @1;

NSNumber *floatNumber = @2.5f;

NSNumber *doubleNumber = @3.14159;

NSNumber *boolNumber = @YES;

NSNumber *charNumber = @'a';

2、字面量数组

NSArray *animals = @[@"dog",@"cat",@"mouse"];

NSString *dog = @[0];

需要注意的是,当用字面量语法创建的数组中的某一个对象为nil时,则会抛出异常,如:

id object1 = /*............*/;

id object2 = /*............*/;   (为nil)

id object3 = /*............*/;

NSArray *arrayA = [NSArray arrayWithObjects:object1, object2, object3, nil];

NSArray *arrayB = @[object1, object2, object3];

arrayA中只含有一个元素,因为arrayWithObjects:会依次处理各个参数,知道碰到nil为止,由于object2为nil,所以该方法会提前结束;

arrayB会抛出异常,便于代码审查;

3、字面量字典

NSDictionary *personData = [NSDictionary dictionaryWithObjectsAndKeys:@"mark", @"name", [NSNumer numberWithInt:28], @"age", nil];<对象><键>

NSDictionary *personData = @{@"name":@"mark", @"age":@28};

如果遇到对象为nil,也会抛出异常;

4、可变数组与字典

[mutableArray replaceObjectAtIndex:1 withObjrct:@"dog"];

[mutableDictionary setObject:@"mark" forKey:@"name"];

对比:

mutableArray[1] = @"dog";

mutableDictionary[@"name"] = @"mark";

5、要点:

*应该使用字面量语法来创建字符串、字典、数组、数值,这样更加简明扼要;

*应该通过取下标操作访问数组下标或字典的键所对应的元素;

*用字面量语法创建字典或者数组时,若值中有nil,则会抛出异常。因此务必保证值中不含nil;

编写高质量iOS代码的52个有效方法1-1的更多相关文章

  1. 编写高质量iOS代码的52个有效方法2-1

    一.变量的定义位置(用{}声明示例变量或者用@property属性声明实例变量) 1.用{}声明示例变量: 此方法生命的实例变量,编译器在编译时,会自动计算其偏移量(表示该变量距离存放对象的内存区域的 ...

  2. 编写高质量JavaScript代码的68个有效方法

    简介: <Effective JavaScript:编写高质量JavaScript代码的68个有效方法>共分为7章,分别涵盖JavaScript的不同主题.第1章主要讲述最基本的主题,如版 ...

  3. Effective Python之编写高质量Python代码的59个有效方法

                                                         这个周末断断续续的阅读完了<Effective Python之编写高质量Python代码 ...

  4. 编写高质量JS代码的68个有效方法(八)

    [20141227]编写高质量JS代码的68个有效方法(八) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  5. 编写高质量JS代码的68个有效方法(七)

    [20141220]编写高质量JS代码的68个有效方法(七) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  6. 编写高质量JS代码的68个有效方法(六)

    [20141213]编写高质量JS代码的68个有效方法(六) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  7. 编写高质量JS代码的68个有效方法(四)

    [20141129]编写高质量JS代码的68个有效方法(四) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  8. 编写高质量JS代码的68个有效方法(三)

    [20141030]编写高质量JS代码的68个有效方法(三) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  9. 编写高质量JS代码的68个有效方法(二)

    [20141011]编写高质量JS代码的68个有效方法(二) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

随机推荐

  1. AngularCSS--关于angularjs动态加载css文件的方法(仅供参考)

    AngularCSS CSS on-demand for AngularJS Optimize the presentation layer of your single-page apps by d ...

  2. LR设置关联---部分内容摘自网络--望见谅

    模拟环境---LR机票定票系统设置:首页点击administration-勾选Set LOGIN form's action tag to an error page.选项,点击update. 现在许 ...

  3. ListView的局部刷新

    有的列表可能notifyDataSetChanged()代价有点高,最好能局部刷新. 局部刷新的重点是,找到要更新的那项的View,然后再根据业务逻辑更新数据即可. private void upda ...

  4. CoreJavaE10V1P3.7 第3章 Java的基本编程结构-3.7 输入输出(Input ,Output)

    3.7.1 读取输入 Scanner in = new Scanner(System.in); System.out.print("What is your name? "); S ...

  5. 提示找不到xml配置文件

    ClassPathXmlApplicationContext("applicationContext.xml")默认文件夹是resouerces,所以要把xml文件放在这个下面.

  6. 让IE的Button自适应文字宽度兼容

    width:auto; overflow:visible; 这样将可以去掉IE按钮文字两边多余的空格,并按照自己设置的padding 来显示

  7. cursor属性

    cursor光标类型 auto default none context-menu help pointer progress wait cell crosshair text vertical-te ...

  8. spring 加载多个资源文件

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Prop ...

  9. background-size (设置背景图片的大小)

    设置背景图片的大小,以长度值或百分比显示(数值包括 长度length和百分比percentage),还可以通过cover和contain来对图片进行伸缩. 语法:background-size: au ...

  10. 学习自动化工具gulp

    <什么是gulp>官网地址:http://gulpjs.com/ gulp是可以自动化执行任务的工具,在开发流程里,一定有一些动作需要手工的重复的去执行,例如: ·把一个文件拷贝到另外一个 ...