在OC中可能经常会遇到 nil,Nil,NULL和NSNull,下面分析一下之间的区别:

Symbol Value Meaning
NULL (void *)0 literal null value for C pointers
nil (id)0 literal null value for Objective-C objects
Nil (Class)0 literal null value for Objective-C classes
NSNull [NSNull null] singleton object used to represent null

一、nil:对象为空

定义某一实例对象为空值。例如:

  1. NSObject* obj = nil;
  2. if (nil == obj) {
  3. NSLog(@"obj is nil");
  4. }
  5. else {
  6. NSLog(@"obj is not nil");
  7. }

二、Nil:类为空

定义某一类为空。例如:

  1. Class someClass = Nil;
  2. Class anotherClass = [NSString class];

三、NULL:基本数据对象指针为空

用于c语言的各种数据类型的指针为空。例如:

  1. intint *pointerToInt = NULL;
  2. charchar *pointerToChar = NULL;
  3. struct TreeNode *rootNode = NULL;

四、NSNull

集合对象无法包含 nil 作为其具体值,如NSArray、NSSet和NSDictionary。相应地,nil 值用一个特定的对象 NSNull 来表示。NSNull 提供了一个单一实例用于表示对象属性中的的nil值。

  1. @interface NSNull : NSObject <NSCopying, NSSecureCoding>
  2. + (NSNull *)null;
  3. @end

在NSNull单例类中,提供了唯一的方法null:Returns the singleton instance of NSNull.

例如:

  1. NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
  2. mutableDictionary[@"someKey"] = [NSNull null]; // Sets value of NSNull singleton for `someKey`
  3. NSLog(@"Keys: %@", [mutableDictionary allKeys]); // @[@"someKey"]

五、说明:

Technically they're all the same, but in practice they give someone reading your code some hints about what's going on; just like naming classes with a capital letter and instances with lowercase is recommended, but not required.

If someone sees you passing NULL, they know the receiver expects a C pointer. If they see nil, they know the receiver is expecting an object. If they see Nil, they know the receiver is expecting a class. Readability.

六、注

下面附带几个有趣的例子:

(1)

  1. NSObject *obj1 = [[NSObject alloc] init];
  2. NSObject *obj2 = [NSNull null];
  3. NSObject *obj3 = [NSObject new];
  4. NSObject *obj4;
  5. NSArray *arr1 = [NSArray arrayWithObjects:obj1, obj2, obj3, obj4, nil nil];
  6. NSLog(@"arr1 count: %ld", [arr1 count]);    //arr1 count: 3
  7. NSObject *obj1;
  8. NSObject *obj2 = [[NSObject alloc] init];
  9. NSObject *obj3 = [NSNull null];
  10. NSObject *obj4 = [NSObject new];
  11. NSArray *arr2 = [NSArray arrayWithObjects:obj1, obj2, obj3, obj4, nil nil];
  12. NSLog(@"arr2 count: %ld", [arr2 count]);   //arr2 count: 0

为啥第一个数组元素有三个,而第二个数组元素为0.先看看:

  1. NSObject* obj;
  2. if (nil == obj) {
  3. NSLog(@"obj is nil");
  4. }
  5. else {
  6. NSLog(@"obj is not nil");
  7. }

这个输出:obj is nil。而NSArray是以nil结尾的。所以知道原因了吧!

(2)

    1. //有异常!
    2. NSObject *obj1 = [NSNull null];
    3. NSArray *arr1 = [NSArray arrayWithObjects:@"One", @"TWO", obj1, @"three" ,nil];
    4. for (NSString *str in arr1) {
    5. NSLog(@"array object: %@", [str lowercaseString]);
    6. }
    7. //修改
    8. NSObject *obj1 = [NSNull null];
    9. NSArray *arr1 = [NSArray arrayWithObjects:@"One", @"TWO", obj1, @"three" ,nil];
    10. for (NSString *str in arr1) {
    11. if (![str isEqual:[NSNull null]]){
    12. NSLog(@"array object: %@", [str lowercaseString]);
    13. }
    14. }

最新文章

  1. git工作区、暂存区、版本库之间的关系
  2. SpringMVC学习系列(9) 之 实现注解式权限验证
  3. http://www.cnblogs.com/xqin/p/4862849.html
  4. VI下删除所有内容
  5. Struts2_搭建环境及HelloWorld
  6. JavaScript 现状:方言篇
  7. Hadoop 安装(1) CENTOS 安装与配置
  8. 所谓编码--泛谈ASCII、Unicode、UTF8、UTF16、UCS-2等编码格式
  9. Cocos2D:塔防游戏制作之旅(十八)
  10. mysql 错误2002
  11. 开源防火墙(pfSense)的安装部署与配置
  12. AngularJS入门基础——表达式
  13. windows下,java环境变量的设置,设置点击startup.bat启动tomcat
  14. A request has been denied as a potential CSRF attack错误解决方法
  15. dp合集 广场铺砖问题&amp;&amp;硬木地板
  16. 20172321 2018-2019《Java软件结构与数据结构》第三周学习总结
  17. mcake活动维护,检查 ★ ★
  18. 基于iscroll实现下拉和上拉刷新
  19. Python函数 __import__()
  20. linux网络编程中的基本概念

热门文章

  1. ARM中断向量表与响应流程【转】
  2. docker安装(2016-08-25版本)
  3. 虚拟机安装苹果系统 VMware 12安装Mac OS X 10.10
  4. Centos7 环境准备
  5. mysql 库操作、存储引擎、表操作
  6. 自己编译生成k8s的rpm包
  7. Java学习笔记(七)——获取类中方法的信息,java的LinkedList
  8. 《The art of software testing》的一个例子
  9. AC日记——国王游戏 洛谷 P1080
  10. 前端读者 | 百度前端编码规范(CSS)