1. Literal Syntax

NSString *someString = @"Effective Objective-C 2.0";

NSNumber *someNumber = [NSNumber numberWithInt:1];

<=>

NSNumber *someNumber = @1;

// syntax also works for expressions

int x = 5;
float y = 6.32f;
NSNumber *expressionNumber = @(x * y);

NSArray *animals =
[NSArray arrayWithObjects:@"cat", @"dog",
@"mouse", @"badger", nil];

<=>

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

NSString *dog = [animals objectAtIndex:1];

<=>

NSString *dog = animals[1];

However, you need to be aware of one thing when creating arrays using the literal
syntax. If any of the objects is nil, an exception is thrown

a scenario:

id object1 = /* ... */;
id object2 = /* ... */;
id object3 = /* ... */;

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

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

Now consider the scenario in which object1 and object3 point to valid Objective-C
objects, but object2 is nil. The literal array, arrayB, will cause the exception to be thrown.
However, arrayA will still be created but will contain only object1. The reason is that the
arrayWithObjects: method looks through the variadic arguments until it hits nil, which is
sooner than expected.

It’s much better that an exception is thrown, causing a probable application crash, rather than creating an array
having fewer than the expected number of objects in it.

// Literal Dictionaries

NSDictionary *personData =
[NSDictionary dictionaryWithObjectsAndKeys:
@"Matt", @"firstName", @"Galloway", @"lastName",
[NSNumber numberWithInt:28], @"age", nil];

<=>

NSDictionary *personData =
@{@"firstName" : @"Matt",
@"lastName" : @"Galloway",
@"age" : @28};

NSString *lastName = [personData objectForKey:@"lastName"];

<=>

NSString *lastName = personData[@"lastName"];

Effective Objective-C 2.0 Reading Notes的更多相关文章

  1. Git for Windows v2.11.0 Release Notes

    homepage faq contribute bugs questions Git for Windows v2.11.0 Release Notes Latest update: December ...

  2. Reading Notes of Acceptance Test Engineering Guide

    The Acceptance Test Engineering Guide will provide guidance for technology stakeholders (developers, ...

  3. ASP.NET Core 1.1.0 Release Notes

    ASP.NET Core 1.1.0 Release Notes We are pleased to announce the release of ASP.NET Core 1.1.0! Antif ...

  4. Redis 5.0.0 releases notes

    Redis 5.0 release notes ======================= ---------------------------------------------------- ...

  5. Yasm 1.3.0 Release Notes

    Yasm 1.3.0 Release Notes http://yasm.tortall.net/releases/Release1.3.0.html Target Audience Welcome ...

  6. WeihanLi.Npoi 1.11.0/1.12.0 Release Notes

    WeihanLi.Npoi 1.11.0/1.12.0 Release Notes Intro 最近 NPOI 扩展新更新了两个版本,感谢 shaka chow 的帮忙和支持,这两个 Feature ...

  7. WeihanLi.Npoi 1.14.0 Release Notes

    WeihanLi.Npoi 1.14.0 Release Notes Intro 周末更新了一下项目,开始使用可空引用类型,并且移除了 net45 的支持,仅支持 netstandard2.0 Cha ...

  8. WeihanLi.Npoi 1.16.0 Release Notes

    WeihanLi.Npoi 1.16.0 Release Notes Intro 最近有网友咨询如何设置单元格样式,在之前的版本中是不支持的,之前主要考虑的是数据,对于导出的样式并没有支持,这个 is ...

  9. iOS开发——技术精华Swift篇&Swift 2.0和Objective-C2.0混编之第三方框架的使用

    swift 语言是苹果公司在2014年的WWDC大会上发布的全新的编程语言.Swift语言继承了C语言以及Objective-C的特性,且克服了C语言的兼容性问题.Swift语言采用安全编程模式,且引 ...

随机推荐

  1. LoadRunner参数数组

    参数数组提供了对一类参数集中存放的机制,其中LR内置的几个函数有:lr_paramarr_idx().lr_paramarr_len().lr_paramarr_random() 同时参数数组必须满足 ...

  2. Collectl基础

    Collectl比nmon更专业和深入的linux性能测试工具 首先是安装 $ sudo apt-get install collectl 直接输入collectl就可以看到数据 默认是CPU,硬盘, ...

  3. 【Linux】一个简单的线程创建和同步的例子

    最近很多精力在Linux上,今天简单看了一下Linux上的线程和同步,其实不管windows还是Linux,OS层面的很多原理和概念都是相同的,很多windows之上的经验和概念完全可以移植到Linu ...

  4. Java 分割文件 注意事项

    public static void main(String args[]) throws Exception { if (args.length < 1) { System.exit(0); ...

  5. effective c++:dynamic_cast,避免返回handles指向对象内部

    关于dynamic_cast 假定我们有一个基类指针bp,我们在运行时需要把它转换成他的派生类指针,这个时候需要用到dynamic_cast. Derived *dp = dynamic_cast&l ...

  6. MySQL Order By Rand()效率

    最近由于需要大概研究了一下MYSQL的随机抽取实现方法.举个例子,要从tablename表中随机提取一条记录,大家一般的写法就是:SELECT * FROM tablename ORDER BY RA ...

  7. 如何杀掉当前正在执行的hadoop任务

    [root@Slave01 ~]# hadoop job -listDEPRECATED: Use of this script to execute mapred command is deprec ...

  8. Having the Result Set of a Stored Proc Sent to You by RSS Feed.

    Having the Result Set of a Stored Proc Sent to You by RSS Feed. by JBrooks   14. 十二月 2010 12:44 I wa ...

  9. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

  10. (转载)ETL利器Kettle实战应用解析系列一【Kettle使用介绍】

    http://www.cnblogs.com/limengqiang/archive/2013/01/16/kettleapply1.html ETL利器Kettle实战应用解析系列一[Kettle使 ...