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. WeifenLuo.WinFormsUI.Docking"的使用 z

    在伍华聪的博客中,看到布局控件"WeifenLuo.WinFormsUI.Docking",发现的确是一个非常棒的开源控件,用过的人都深有体会,该控件之强大.美观.不亚于商业控件. ...

  2. iOS 复选框风格转换 Switchery 开关效果

    Switchery 是个简单的 JavaScript 组件,只要几个简单的步骤就可以帮助用户把默认的 HTML 复选框转换成漂亮 iOS 7 样式风格.用户可以很方便的自定义这种转换,所以可以很好的配 ...

  3. JAVA & Android 系统环境变量配置

    Java JAVA_HOME:C:\Program Files\Java\jdk1.7.0_40 CLASSPATH:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\ ...

  4. CListCtrl总结.xml

    pre{ line-height:1; color:#d1653c; background-color:#000000; font-size:16px;}.sysFunc{color:#566d68; ...

  5. oracle 定时任务(DBMS_JOB)

    示例如下: -- 每隔一分钟执行存储过程p1 create or replace procedure p2 as job_num number; begin dbms_job.submit(job_n ...

  6. PASCAL相关图书推荐

    PASCAL程序设计(第2版) 作      者 郑启华 著 出 版 社 清华大学出版社 出版时间 2013-01-01 版      次 2 页      数 286 印刷时间 2013-01-01 ...

  7. Linux 的 screen用法

    screen可以将任务挂起,即将任务放在后台,一般5个任务左右. 1.新建screen会话:直接输入screen命令或者screen -S [会话名称] 2.退出会话:按下组合键Ctrl+a并松开,此 ...

  8. 修改docker的默认存储位置

    service docker stop mv /var/lib/docker /mnt/docker ln -s /mnt/docker /var/lib/docker ls /var/lib/doc ...

  9. Eclipse或Myeclipse常用快捷键组合详解

    Eclipse 是一个开放源代码的.基于Java的可扩展开发平台,就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境.. Eclipse(Myeclipse)中有很多便于开发的快捷键 ...

  10. cannot restore segment prot after reloc: Permission denied

    编辑/etc/selinux/config,找到这段:# This file controls the state of SELinux on the system. # SELINUX= can t ...