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的更多相关文章

  1. Objective-C 中 NULL、nil、Nil、NSNull 的定义及不同

    本文由我们团队的 康祖彬 童鞋撰写,这是他的个人主页:https://kangzubin.cn. 理解"不存在"的概念不仅仅是一个哲学的问题,也是一个实际的问题.我们是有形宇宙的居 ...

  2. iOS中使用nil NULL NSNULL的区别

    nil NULL NSNULL的区别主要以下几点 1.nil:一般赋值给空对象 2.NLL:一般赋值给nil之外的其他空值.入SEL等. 3.NSULL:NSNULL只有一种方法+ (NSNull * ...

  3. nil/Nil/NULL/NSNull

    nil/Nil/NULL/NSNull的区别 一个简单的小例子: NSObject *obj = nil; NSLog(@"%@",obj); =>null NSObject ...

  4. 黑马程序员-nil Nil NULL NSNull 野指针和空指针

    空指针1.空指针指不含有任何内存地址的指针.在没有具体初始化之前,其被符值为0Dog * dog = nil;Dog * dog = NULL;都为空指针2.野指针指指向的内存为垃圾内存,导致其值不确 ...

  5. IOS 学习笔记 2015-03-20 O之 nil,Nil,NULL,NSNull

    1.oc最好 用nil   [ nil  任意方法],不会崩溃 nil 是一个对象值.NULL是一个通用指针(泛型指针). 2. NSNULL,NULL和nil在本质上应该是一样的,NULL和nil其 ...

  6. NULL、nil、Nil、NSNull的区别

    标志 值 含义 NULL (void *)0 C指针的字面零值 nil (id)0 Objecve-C对象的字面零值 Nil (Class)0 Objecve-C类的字面零值 NSNull [NSNu ...

  7. nil Nil NULL NSNull 之间的区别

    nil -> Null-pointer to objective- c objectNIL -> Null-pointer to objective- c class  表示对类进行赋空值 ...

  8. iOS下nil 、NULL、 Nil 、NSNull的区别

    1.nil,定义一个空的实例,指向OC中对象的空指针. 示例代码: NSString *someString = nil; NSURL *someURL = nil; id someObject = ...

  9. iOS中nil 、NULL、 Nil 、NSNull

    nil,定义一个空的实例,指向OC中对象的空指针. 示例代码: NSString *someString = nil; NSURL *someURL = nil; id someObject = ni ...

随机推荐

  1. minimum-genetic-mutation

    题目还是很好的,提供了一种新的思路方向. 细节方面,开始我的判断条件用的dict,不太好,后来debug好了. 另外,注意其中用数组初始化Set的方法:Set<String> dict = ...

  2. poj 1195 mobile phone

    题目连接: 题意:要求设计这样一个数据结构,支持下列操作 1.add(x,y,a).对二维数组的第x行,第y列加上a. 2.sum(l,b,r,t).求所有满足l<=x<=r,b<= ...

  3. 使用C++读写Excel

    1.导入Excel类型库 使用Visual C++的扩展指令#import导入Excel类型库: 1 2 3 4 5 6 7 8 9 10 11 12 #import "C:\\Progra ...

  4. BOM浏览器对象

    BOM 浏览器对象 一.浏览器本身就自己有一些对象,不用创建就可以使用 window(当前浏览器窗体) 属性: status opener closed parent top 方法: alert(); ...

  5. [转] gc tips(3)

    原文地址:http://kevincao.com/2011/08/actionscript-garbage-collection-2/ 谈谈ActionScript垃圾回收(下) 前文我们介绍了GC的 ...

  6. Heritrix源码分析(八) Heritrix8个处理器(Processor)介绍(转)

    本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/643367       本博客已迁移到本人独立博客: http://www.yun5u ...

  7. 添加navbar以及上面的左右按钮代码

    UINavigationBar *navBar = [[UINavigationBaralloc] initWithFrame:CGRectMake(0, 0, 824, 44)]; navBar.b ...

  8. pg psql命令

    linux下使用psql命令操作数据库 下面主要用到了insert into  ,pg_dump  , pg_restore 命令 按步骤走 su postgres                   ...

  9. T-SQL备忘(4):分页

    set statistics io on set statistics time on --SQL Server 2012分页方式 select * from Production.Product o ...

  10. 一天一点MySQL复习——获取数据库系统时间、变量赋值、变量比较

    一.SQL获取系统时间 mysql> select now() from dual; +---------------------+ | now() | +------------------- ...