在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:对象为空

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

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

二、Nil:类为空

定义某一类为空。例如:

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

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

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

[objc] view plaincopy
  1. intint *pointerToInt = NULL;
  2. charchar *pointerToChar = NULL;
  3. struct TreeNode *rootNode = NULL;

四、NSNull

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

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

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

例如:

[objc] view plaincopy
  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)

[objc] view plaincopy
  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.先看看:

[objc] view plaincopy
  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)

[objc] view plaincopy
  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. }

转载于:https://www.cnblogs.com/sanjianghuiliu/p/4075492.html

Objective-c nil, Nil, NULL和NSNull的区别相关推荐

  1. 关于nil和 null和NSNull的相关问题

    2019独角兽企业重金招聘Python工程师标准>>> 关于nil和 null和NSNull的区别及相关问题 1.nil和null从字面意思来理解比较简单,nil是一个对象,而NUL ...

  2. 【iOS】—— nil、Nil、NULL和NSNull学习

    nil.Nil.NULL和NSNull 文章目录 nil.Nil.NULL和NSNull nil NSNull Nil NULL 总结: 我们先来看看这几个苹果官方文档的解释: nil:Defines ...

  3. iOS——nil、Nil、NULL和[NSNull null]的区别

    iOS--nil.Nil.NULL和[NSNull null]的区别 首先我们先来看一下苹果文档上的解释: nil:Defines the id of a null instance.(定义空实例的i ...

  4. Objective C 中的nil,Nil,NULL和NSNull理解

    kenyo网友的原创说法是:做IOS开发的估计都对Objective-C的内存管理机制很头疼,一不小心程序就会出内存泄露,我也不例外,前几天被指针的置nil与release给搞惨了,今和大家详细解说一 ...

  5. ios nil、NULL和NSNull 的使用

    nil用来给对象赋值(Objective-C中的任何对象都属于id类型),NULL则给任何指针赋值,NULL和nil不能互换,nil用于类指针赋值(在Objective-C中类是一个对象,是类的met ...

  6. Objective-c 中 nil, Nil, NULL和NSNull的区别

    nil: A null pointer to an Objective-C object. ( #define nil ((id)0)  ) nil表示一个Objective-C对象,这个对象的指针指 ...

  7. NIL、NIL、NULL和NSNULL区别

    前言 记得曾经有不少朋友问过笔者,在Objective-C中nil和Nil以及NULL的区别.最重要的是,在面试中还有不少朋友常会被问到.记得当年刚找工作的时候,笔者就被面试官问到过,现在笔者在这里统 ...

  8. nil、Nil、NULL和NSNull学习

    nil 我们使用nil表示Objective-C对象为空,如NSString *str = nil.nil就代表((void *)0) 我们给对象赋值时一般会使用object = nil,表示我想把这 ...

  9. iOS开发 nil Nil NULL NSNull 的使用

    nil: A null pointer to an Objective-C object. ( #define nil ((id)0) ) nil 是一个对象值. Nil: A null pointe ...

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

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

最新文章

  1. 作为产品经理,你需要了解的基本算法知识和实操
  2. java线程同步的死锁_Java基础之线程5-线程同步死锁
  3. 数据结构与算法专题——第十二题 Trie树
  4. PHP笔记-文件上传例子
  5. 手机MODEM 开发(32)--VOLTE 信令
  6. 手机用久了很卡怎么办?
  7. 阿里京东被怼假货泛滥;谷歌 CEO 承认中国版搜索 App 存在;YouTube 全球宕机 | 极客头条...
  8. c++ 空类大小不为0的原因
  9. bagging boosting 随机森林 GBDT对比
  10. 【雷达通信】基于mtatlab距离多普勒(RD)、CS、RM算法机载雷达成像【含Matlab源码 284期】
  11. 什么是TOC约束理论以及TOC系统业务流程
  12. wps底板颜色怎么去掉_wps怎么把复制网页的灰色阴影去掉 底纹去掉方法
  13. winfrom MonthCalendar的mousedown方法 获得 选择日期
  14. Vue-cli的安装与基本操作
  15. Android DNS之gethostbyname()的实现
  16. Ubuntu双系统没有WiFi适配器、不能连接WiFi
  17. Spring AOP术语
  18. 【pureftp】解决filezilla连接ftp报错 服务器发回了不可路由的地址 使用服务器地址代替
  19. nimble源码学习——广播流程源码分析1
  20. rabbitmq链接超时_RabbitMQ前置SLB中TCP连接超时900秒限制

热门文章

  1. CVE2014-6287分析报告
  2. Windows系列服务器上配置JSP运行环境,以及网站上线
  3. 在android 采用 android junit test 测试注意
  4. 动态ARP检测,引发上网断断续续
  5. 选择checkbox显示隐藏内容
  6. MOSS User Profile(三):查看用户配置文件修改历史
  7. 老婆生病了,后果很严重!
  8. Ubuntu18.04 如何解决编译objective-c出现undefined reference to objc_get_class
  9. Android Studio创建安卓虚拟机并测试app
  10. BZOJ1086[SCOI2005] 王室联邦