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. Machine Learning for hackers读书笔记(五)回归模型:预测网页访问量

    线性回归函数 model<-lm(Weight~Height,data=?) coef(model):得到回归直线的截距 predict(model):预测 residuals(model):残 ...

  2. HDU 1728 逃离迷宫【BFS】

    题意:给出一个起点,一个终点,规定的转弯次数,问能否在规定的转弯次数内到达终点--- 这一题是学(看)习(题)的(解)@_@ 主要学了两个地方 一个是剪枝,如果搜到的当前点的转弯次数小于该点turn数 ...

  3. mongodb主从复制

    1)主服务器--master --port 20001 2)从服务器--slave --source 127.0.0.1:20001 --port 20002 注释:--master 以主服务器形式启 ...

  4. mongodb用户授权

    1)登录admin 数据库,admin是隐藏的数据库,为mongodb的超级管理员数据表mongo admin新建用户db.createUser({'user':'test','pwd':'12345 ...

  5. UML和模式应用

    引言 Applying UML and Patterns,以一个商店POS系统NextGen和一个掷骰子游戏Monopoly为例,围绕OOA/D的基本原则GRASP,以迭代作为基本方法.以UML为表达 ...

  6. 纯CSS3大转盘抽奖(响应式、可配置)

    源于前段时候微信小程序最初火爆公测时段,把以前用 Canvas 实现的大转盘抽奖移植成微信小程序,无奈当时小程序对 Canvas 支持不够完善,只好降低用 CSS3 实现.虽然比不上 Canvas 绘 ...

  7. Annotation(jdk5.0注解)复习(转自http://3w_cnblogs_com/pepcod/)

    package annotation.test; import java.lang.annotation.ElementType; import java.lang.annotation.Retent ...

  8. 【英语】Bingo口语笔记(27) - 如何培养口语语感

  9. oracle----sqlldr用法

    SQL*LOADER是ORACLE的数据加载工具,通常用来将操作系统文件迁移到ORACLE数据库中.SQL*LOADER是大型数据仓库选择使用的加载方法,因为它提供了最快速的途径(DIRECT,PAR ...

  10. Java正确转换html编码

    Java 中能將 html 編碼正確轉換的套件: org.apache.commons.lang.StringEscapeUtils. String source = "The less t ...