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. YII Framework学习教程-YII的日志

    日志的作用(此处省略1000字) YII中的日志很好很强大,允许你把日志信息存放到数据库,发送到制定email,存放咋文件中,意见显示页面是,甚至可以用来做性能分析. YII中日志的基本配置:/yii ...

  2. 长轮询和Comet

    长轮询方式是由前端定时发起AJAX请求,若请求到数据则把数据显示出来. comet方式是由客户端与服务器端发起一个长连接,然后客户端通过监听事件的方式,来对服务器端返回的数据作出响应和处理. 实时性要 ...

  3. <转>安卓应用测试checklist

    启动: 1. 启动入口:桌面正常启动,最近运行启动,所有程序列表中启动,锁屏快捷启动 2. 其他入口:从其他程序开启应用,从外部以文件形式打开应用(如果有) 3. 退回:从其他程序退回时回到被测应用, ...

  4. 【LeetCode】58 - Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  5. 数往知来 ADO.NET <八>

    ADO.NET基础 学习目的:通过程序访问数据库 ,ADO.NET就是一组类库, -->connection   用来连接数据库的类 语法:首先需要一个连接字符串 -->以SQL serv ...

  6. Fedora20 和ubuntu 14.04 chrome标签中文乱码

    作为两个流行的桌面发行版本,Fedora和ubuntu最新版本都存在chrome标签中文乱码问题. 下面是解决办法,都来自百度贴吧. 1.ubuntu 系列: 解决办法就是: 编辑/etc/fonts ...

  7. PHPCMS V9实现硬件地址MAC绑定访问技术实现

    目的:会员登录需要 用户名.密码.身份识别码(新增字段) 效果:  解决方法: 目前数据库中macaddress字段已经添加,修改了phpcms\modules\member\index.php 63 ...

  8. 使用Ambari部署hadoop集群

    准备工作 1. 设置sudo免密码登陆 由于CentOS安装之后,普通用户无sudo权限,故应该设置sudo权限. 参考文章:http://www.cnblogs.com/maybob/p/32988 ...

  9. poj 3094 Quicksum

    #include <stdio.h> #include <string.h> ]; int main() { ; int i,len; while(gets(word)) { ...

  10. SCAU 10893 Spiral

    10893 Spiral 时间限制:1000MS  内存限制:65535K 题型: 编程题   语言: 无限制 Description Given an odd number n, we can ar ...