Effective Objective-C 2.0 Reading Notes
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的更多相关文章
- Git for Windows v2.11.0 Release Notes
homepage faq contribute bugs questions Git for Windows v2.11.0 Release Notes Latest update: December ...
- Reading Notes of Acceptance Test Engineering Guide
The Acceptance Test Engineering Guide will provide guidance for technology stakeholders (developers, ...
- 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 ...
- Redis 5.0.0 releases notes
Redis 5.0 release notes ======================= ---------------------------------------------------- ...
- Yasm 1.3.0 Release Notes
Yasm 1.3.0 Release Notes http://yasm.tortall.net/releases/Release1.3.0.html Target Audience Welcome ...
- 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 ...
- WeihanLi.Npoi 1.14.0 Release Notes
WeihanLi.Npoi 1.14.0 Release Notes Intro 周末更新了一下项目,开始使用可空引用类型,并且移除了 net45 的支持,仅支持 netstandard2.0 Cha ...
- WeihanLi.Npoi 1.16.0 Release Notes
WeihanLi.Npoi 1.16.0 Release Notes Intro 最近有网友咨询如何设置单元格样式,在之前的版本中是不支持的,之前主要考虑的是数据,对于导出的样式并没有支持,这个 is ...
- iOS开发——技术精华Swift篇&Swift 2.0和Objective-C2.0混编之第三方框架的使用
swift 语言是苹果公司在2014年的WWDC大会上发布的全新的编程语言.Swift语言继承了C语言以及Objective-C的特性,且克服了C语言的兼容性问题.Swift语言采用安全编程模式,且引 ...
随机推荐
- hbm.xml支持的类型
- table应用之colspan与rowspan
<table border=" borderColorDark="#66ff33"> <tr> <td rowspan=" ali ...
- HTTP协议的状态码
对于Web编程人员来说,熟悉了解HTTP协议的状态码是很有必要的,很多时侯可能根据HTTP协议的状态码很快就能定位到错误信息!今天整理了一下所有HTTP状态码. HTTP状态码(HTTP Status ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 select_from_list(self, locator, *items)
def select_from_list(self, locator, *items): """Selects `*items` from list identified ...
- DTD 简介
文档类型定义(DTD)可定义合法的XML文档构建模块.它使用一系列合法的元素来定义文档的结构.DTD 可被成行地声明于 XML 文档中,也可作为一个外部引用. 内部的 DOCTYPE 声明 假如 DT ...
- 基于vagrant工具在win7下免密登录linux
一.SSH加密方式 SSH采用的是"非对称密钥系统",即耳熟能详的公钥私钥加密系统,其安全验证又分为两种级别. 1. 基于口令的安全验证 这种方式使用用户名密码进行联机登录,一般情 ...
- Eclipse下建立geoserver源码工程
摘要:本文详细阐述,如何基于geoserver源码构建eclipse工程文件,操作过程中除用到jdk.eclipse以外,还有git和maven,操作系统为windows8. 1安装Git 从(htt ...
- js混淆工具
1\ http://www.jasob.com 2\ http://developer.yahoo.com/yui/compressor
- <一道题>abc+cba=1333,求满足条件的abc的值,隐含条件a!=0,c!=0
这类东西,无非就是穷举法.见下面代码: #include <stdio.h> #include <stdlib.h> /* *abc + cba = 1333 * *a = ? ...
- linux du命令: 显示文件、目录大小
介绍:du命令用于显示指定文件(夹)在磁盘中所占的空间信息.假如指定的文件参数实际上是一个目录,就要计算该目录下的所有文件.假如 没有提供文件参数,执行du命令,显示当前目录内的文件占用空间信息. 语 ...