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. [Everyday Mathematics]20150304

    证明: $$\bex \frac{2}{\pi}\int_0^\infty \frac{1-\cos 1\cos \lm-\lm \sin 1\sin \lm}{1-\lm^2}\cos \lm x\ ...

  2. [转]Loadrunner11之VuGen运行时设置Run-Time Setting

    转自:http://www.51testing.com/html/92/450992-248065.html General 1.Run Logic运行逻辑 脚本如何运行的,每个action和acti ...

  3. 查找指定目录下的文件 .xml

    pre{ line-height:1; color:#9f1d66; background-color:#cfe4e4; font-size:16px;}.sysFunc{color:#5d57ff; ...

  4. 笔记:C语言数据类型在32位与64位机器上的字节数

    读<深入理解计算机系统> 第二章 信息的表示与处理 32位与64位的典型值,单位字节 声明 32位机器 64位机器 char 1 1 short int int 4 4 long int ...

  5. FILETIME, SYSTEMTIME 与 time_t 相互转换

    FILETIME, SYSTEMTIME 与 time_t 相互转换 2009-08-24 15:37:14|  分类: 默认分类|举报|字号 订阅     //******************* ...

  6. 使用Java程序调用MatLab

    Java代码实现的计算难免会显得不够高效.而利用MATLAB写好相应的计算函数,然后打包成jar包供Java调用,在某些情况下会更加方便.或者有些时候会涉及到使用Java调用MatLab展现一些二维三 ...

  7. 深刻理解Python中的元类metaclass(转)

    本文由 伯乐在线 - bigship 翻译 英文出处:stackoverflow 译文:http://blog.jobbole.com/21351/ 译注:这是一篇在Stack overflow上很热 ...

  8. JXSE and Equinox Tutorial, Part 1

    http://java.dzone.com/articles/jxse-and-equinox-tutorial-part —————————————————————————————————————— ...

  9. 关于网上流传的四个原版Windows XP_SP2全面了解

    如何查看你的XP SP2是否原版?打开Windows/System32/找到EULA这个文本文档(即eula.txt):打开在最后一行:有一个EULAID:XPSP2_RM.0_PRO_RTL_CN ...

  10. 我所改造的JSocket适用于任何DELPHI版本

    JSOCKET是异步选择模式的通信控件,简单而强大,传奇的早期版本就是使用它作通信. { ******************************************************* ...