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 ...
随机推荐
- Mac下配置idk
Mac下配置java #以下进入啰嗦模式演示添加jdk7 #下载jdk7的mac版 #官网下载地址http://www.oracle.com/technetwork/java/javase/downl ...
- js之dom_1
DOM ie中有dom对象都是com对象的形式实现的 操作dom时,要注意返回的节点列表.属性列表都是动态的,会随着操作的改变而实时改变 document.getElementByI ...
- 用Visio画UML用例图
1.用例图 用例图描述参与者所理解的系统功能.主要元素是用例和参与者. 用例图的4个基本组件:参与者(Actor).用例(Use Case).关系(Relationship)和系统. 下面以银行储蓄系 ...
- 重装yum
1)卸载yum rpm -aq|grep yum|xargs rpm -e --nodeps 2)下载并安装python-urlgrabber,python-pycurl,yum-metadata-p ...
- 《C#高级编程》之泛型--1创建泛型类
.NET自从2.0版本开始就支持泛型. 非泛型链表 闲话休提,马上来看下非泛型的简化链表类,它可以包含任意类型的对象. LinkedListNode.cs中: 在链表中,一个元素引用另一个元素,所以必 ...
- Cython:基础教程(1) 语法
1 变量定义 http://docs.cython.org/src/reference/language_basics.html http://blog.csdn.net/i2cbus/article ...
- 当sql报错代码,不允许对表操作的原因
不允许对表 表名 执行操作,原因码为 原因码. 说明 限制对表 表名 的访问.原因基于下列原因码 原因码: 1 该表处于“设置完整性暂挂无访问”状态.未强制表的完整性并且表的内容可能是无效的.如果从属 ...
- github 开源项目
项目地址: https://github.com/Trinea/android-open-project
- 参考:iPhone OS 3.0中的字体列表
字体是我们在iPhone开发中经常需要用到的.但是iPhone里面到底内置了哪些字体呢?下面就是一个常用的列表. Family name: AppleGothic Font name: AppleGo ...
- myeclipse10添加jQuery自动提示
首先先要在装上spket插件,这个网上有好多教程,我就不详细说了,主要说一下后面的设置,因为我发现我按照网上的装完也设置完没办法使用自动提示功能,以下是我根据前辈的经验然后自己摸索出来的: 选中所建的 ...