Represent nil with NSNull
【Represent nil with NSNull】
It’s not possible to add nil to the collection classes described in this section because nil in Objective-C means “no object.” If you need to represent “no object” in a collection, you can use the NSNull class:
NSArray *array = @[ @"string", @, [NSNull null] ];
NSNull is a singleton class, which means that the null method will always return the same instance. This means that you can check whether an object in an array is equal to the shared NSNull instance:
for (id object in array) {
if (object == [NSNull null]) {
NSLog(@"Found a null object");
}
}
【Working with Blocks】
Blocks can also take arguments and return values just like methods and functions.As an example, consider a variable to refer to a block that returns the result of multiplying two values:
double (^multiplyTwoValues)(double, double);
The corresponding block literal might look like this:
^ (double firstValue, double secondValue) {
return firstValue * secondValue;
}
The firstValue and secondValue are used to refer to the values supplied when the block is invoked, just like any function definition. In this example, the return type is inferred from the return statement inside the block.
If you prefer, you can make the return type explicit by specifying it between the caret and the argument list:
^ double (double firstValue, double secondValue) {
return firstValue * secondValue;
}
Once you’ve declared and defined the block, you can invoke it just like you would a function:
double (^multiplyTwoValues)(double, double) =
^(double firstValue, double secondValue) {
return firstValue * secondValue;
}; double result = multiplyTwoValues(,); NSLog(@"The result is %f", result);
【Use __block Variables to Share Storage】
If you need to be able to change the value of a captured variable from within a block, you can use the __block storage type modifier on the original variable declaration. This means that the variable lives in storage that is shared between the lexical scope of the original variable and any blocks declared within that scope.
__block int anInteger = ;
void (^testBlock)(void) = ^{
NSLog(@"Integer is: %i", anInteger);
};
anInteger = ;
testBlock();
Because anInteger is declared as a __block variable, its storage is shared with the block declaration. This means that the log output would now show:
Integer is:
It also means that the block can modify the original value, like this:
__block int anInteger = ;
void (^testBlock)(void) = ^{
NSLog(@"Integer is: %i", anInteger);
anInteger = ;
};
testBlock();
NSLog(@"Value of original variable is now: %i", anInteger);
This time, the output would show:
Integer is:
Value of original variable is now:
Represent nil with NSNull的更多相关文章
- Objective-C 中 NULL、nil、Nil、NSNull 的定义及不同
本文由我们团队的 康祖彬 童鞋撰写,这是他的个人主页:https://kangzubin.cn. 理解"不存在"的概念不仅仅是一个哲学的问题,也是一个实际的问题.我们是有形宇宙的居 ...
- iOS中使用nil NULL NSNULL的区别
nil NULL NSNULL的区别主要以下几点 1.nil:一般赋值给空对象 2.NLL:一般赋值给nil之外的其他空值.入SEL等. 3.NSULL:NSNULL只有一种方法+ (NSNull * ...
- nil/Nil/NULL/NSNull
nil/Nil/NULL/NSNull的区别 一个简单的小例子: NSObject *obj = nil; NSLog(@"%@",obj); =>null NSObject ...
- 黑马程序员-nil Nil NULL NSNull 野指针和空指针
空指针1.空指针指不含有任何内存地址的指针.在没有具体初始化之前,其被符值为0Dog * dog = nil;Dog * dog = NULL;都为空指针2.野指针指指向的内存为垃圾内存,导致其值不确 ...
- IOS 学习笔记 2015-03-20 O之 nil,Nil,NULL,NSNull
1.oc最好 用nil [ nil 任意方法],不会崩溃 nil 是一个对象值.NULL是一个通用指针(泛型指针). 2. NSNULL,NULL和nil在本质上应该是一样的,NULL和nil其 ...
- NULL、nil、Nil、NSNull的区别
标志 值 含义 NULL (void *)0 C指针的字面零值 nil (id)0 Objecve-C对象的字面零值 Nil (Class)0 Objecve-C类的字面零值 NSNull [NSNu ...
- nil Nil NULL NSNull 之间的区别
nil -> Null-pointer to objective- c objectNIL -> Null-pointer to objective- c class 表示对类进行赋空值 ...
- iOS下nil 、NULL、 Nil 、NSNull的区别
1.nil,定义一个空的实例,指向OC中对象的空指针. 示例代码: NSString *someString = nil; NSURL *someURL = nil; id someObject = ...
- iOS中nil 、NULL、 Nil 、NSNull
nil,定义一个空的实例,指向OC中对象的空指针. 示例代码: NSString *someString = nil; NSURL *someURL = nil; id someObject = ni ...
随机推荐
- An AnnotationConfiguration instance is required to use
An AnnotationConfiguration instance is required to use <mapping class="jebe7282/study/hibern ...
- SQLite及ORMlite在WebApp中的使用
Spring 配置 下面的databaseUrl在windows下,指向了c:/user/yourhome路径,暂时没想到怎么配置到WEBAPP根路径下. 因为是轻量级工控webapp,数据库规模不大 ...
- 64位Ubuntu 13.04 安装Bochs 2.3.5
bochs 2.3.5源码编译 网上编译bochs的资料非常多,基本的问题都有解决方案,我重点讲不常见的问题. 基本安装步骤 tar vxzf bochs-2.3.5.tar.gz cd bochs- ...
- linux嵌入式系统驱动程序的阻塞与异步
对于那些需要进程独占的设备,需要使用linux提供的阻塞编程.步骤如下: 1.在设备驱动程序中定义该设备的进程等待列多,并将其初始化 static wait_queue_head_t wait_que ...
- HDU 3448 Bag Problem
这是一道搜索的背包题目 题意: 有n件物品从中最多选m件,使其总重量不超过v,求能获得的最大重量 有一个很重要的剪枝(是数据的问题还是这个剪枝本身很高效?): 如果重量最大m件物品都不超过v,则答案就 ...
- 在ACCESS中创建数据库和查询(ACCESS 2000)
备份还原数据库 备份.还原 —— 复制\粘贴 压缩修复数据库命令 —— 复制该文件并重新组织,并重新组织文件在磁盘上的储存方式.压缩同时优化了Access数据库的性能.(工具——实用数据库工具或者工具 ...
- 【转】PHP字符转义相关函数小结
文章中有不正确的或者说辞不清的地方,麻烦大家指出了--- 与PHP字符串转义相关的配置和函数如下: 1.magic_quotes_runtime 2.magic_quotes_gpc 3.addsla ...
- zz github配置
First : 安装:ubuntu 下,终端输入命令: sudo apt-get install git-core git-gui git-doc Next : 设置SSH Key 检查是否已经有SS ...
- C#调用WebService实现天气预报 http://www.webxml.com.cn
C#调用WebService实现天气预报 2011-02-21 14:24:06 标签:天气预报 休闲 WebServices 职场 C# 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始 ...
- LoadRunner中常见参数和变量
1.参数和字符串变量的交换 ①lr_save_string(“hello world”,“param”) 将hello world 保存在参数 param中 ②lr_eval_stri ...