ios nil Nil Null NSNull的区别

本文详细解析了Objective-C中nil、Nil、NULL和NSNull的区别及应用场景,并通过实例展示了这些概念如何影响代码行为。

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

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

一、nil:对象为空

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

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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 plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Class someClass = Nil;  
  2.         Class anotherClass = [NSString class];  


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

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

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. intint *pointerToInt = NULL;   
  2. charchar *pointerToChar = NULL;   
  3. struct TreeNode *rootNode = NULL;  


四、NSNull

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

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @interface NSNull : NSObject <NSCopying, NSSecureCoding>  
  2.   
  3. + (NSNull *)null;  
  4.   
  5. @end  

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

例如:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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 plain  copy
  在CODE上查看代码片 派生到我的代码片
  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.   
  8.   
  9.         NSObject *obj1;  
  10.         NSObject *obj2 = [[NSObject alloc] init];  
  11.         NSObject *obj3 = [NSNull null];  
  12.         NSObject *obj4 = [NSObject new];  
  13.         NSArray *arr2 = [NSArray arrayWithObjects:obj1, obj2, obj3, obj4, nil nil];  
  14.         NSLog(@"arr2 count: %ld", [arr2 count]);   //arr2 count: 0  

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

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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 plain  copy
  在CODE上查看代码片 派生到我的代码片
  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.         //修改  
  9.         NSObject *obj1 = [NSNull null];  
  10.         NSArray *arr1 = [NSArray arrayWithObjects:@"One"@"TWO", obj1@"three" ,nil];  
  11.         for (NSString *str in arr1) {  
  12.             if (![str isEqual:[NSNull null
课程设计报告:总体方案设计说明 一、软件开发环境配置 本系统采用C++作为核心编程语言,结合Qt 5.12.7框架进行图形用户界面开发。数据库管理系统选用MySQL,用于存储用户数据与小精灵信息。集成开发环境为Qt Creator,操作系统平台为Windows 10。 二、窗口界面架构设计 系统界面由多个功能模块构成,各模块职责明确,具体如下: 1. 起始界面模块(Widget) 作为应用程序的入口界面,提供初始导航功能。 2. 身份验证模块(Login) 负责处理用户登录与账户注册流程,实现身份认证机制。 3. 游戏主大厅模块(Lobby) 作为用户登录后的核心交互区域,集成各项功能入口。 4. 资源管理模块(BagWidget) 展示用户持有的全部小精灵资产,提供可视化资源管理界面。 5. 精灵详情模块(SpiritInfo) 呈现选定小精灵的完整属性数据与状态信息。 6. 用户名录模块(UserList) 系统内所有注册用户的基本信息列表展示界面。 7. 个人资料模块(UserInfo) 显示当前用户的详细账户资料与历史数据统计。 8. 服务器精灵选择模块(Choose) 对战准备阶段,从服务器可用精灵池中选取参战单位的专用界面。 9. 玩家精灵选择模块(Choose2) 对战准备阶段,从玩家自有精灵库中筛选参战单位的操作界面。 10. 对战演算模块(FightWidget) 实时模拟精灵对战过程,动态呈现战斗动画与状态变化。 11. 对战结算模块(ResultWidget) 对战结束后,系统生成并展示战斗结果报告与数据统计。 各模块通过统一的事件驱动机制实现数据通信与状态同步,确保系统功能的连贯性与数据一致性。界面布局遵循模块化设计原则,采用响应式视觉方案适配不同显示环境。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值