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. 【转载】Javascript中的this关键字

    看了这篇文章 http://www.ruanyifeng.com/blog/2010/04/using_this_keyword_in_javascript.html 分情况讨论. 情况一:纯粹的函数 ...

  2. EF5&MVC4 学习1、创建新的Contoso University Application,并创建Model Class 生成对应的database

    参考:http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/creating-an-entity-framewo ...

  3. C语言之内存覆盖

    在实现memcpy函数的时候,我们说过要考虑内存覆盖的问题,到底什么是内存覆盖呢,他的出现对程序到底有什么影响呢?我们又要如何去解决这种问题的发生? 首先先看一般人经常实现的memcpy函数: #in ...

  4. JavaScript——关于字符串的replace函数中的function函数的参数

    <!DOCTYPE> <html> <head> </head> <body> <script type="text/jav ...

  5. Servlet的延迟加载和预加载

    我们什么时候使用了延迟加载呢? 先从hibernate引入这个概念吧. hibernate使用lazy属性设置延迟加载,load方法会使用延迟加载. 举个例子: 一个学生有多部手机,如果使用了延迟加载 ...

  6. github.io hexo 安装

    /***************************************************************** * github.io hexo 安装 * 说明: * 本文记录h ...

  7. scala学习笔记(5)

    偏应用函数 举个例子 def sum(a: Int, b: Int, c: Int) = a + b + c val a = sum _ println(a(1,2,3)) 实际发生的事情是这样的:名 ...

  8. XE7 - 升级及初步使用

    春节没抢到回家的票,正好有时间把Delphi2010升级到了XE7. 用了快一个月了,今天算是补记. 安装包用了lsuper大侠整理的lsuper.XE7.Update1.v10.1.拜谢!比较顺利的 ...

  9. js判断是否是pc

    //判断是否是pc function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = new Array("An ...

  10. 大数据分析的众包平台—Kaggle

    众包(Jeff Howe,2006)是一种在互联网蓬勃发展的背景下产生的一种创新的生产组织形式.在这样的商业模式下,企业利用网络将工作分配出去,通过让更合适的人群参与其中来发现创意和解决技术问题.比较 ...