Objective-c nil, Nil, NULL和NSNull的区别
2014-02-07 00:15
471 查看
在OC中可能经常会遇到 nil,Nil,NULL和NSNull,下面分析一下之间的区别:
一、nil:对象为空
定义某一实例对象为空值。例如:
二、Nil:类为空
定义某一类为空。例如:
三、NULL:基本数据对象指针为空
用于c语言的各种数据类型的指针为空。例如:
四、NSNull
集合对象无法包含 nil 作为其具体值,如NSArray、NSSet和NSDictionary。相应地,nil 值用一个特定的对象 NSNull 来表示。NSNull 提供了一个单一实例用于表示对象属性中的的nil值。
在NSNull单例类中,提供了唯一的方法null:Returns the singleton instance of NSNull.
例如:
五、说明:
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)
NSObject *obj1 = [[NSObject alloc] init];
NSObject *obj2 = [NSNull null];
NSObject *obj3 = [NSObject new];
NSObject *obj4;
NSArray *arr1 = [NSArray arrayWithObjects:obj1, obj2, obj3, obj4, nil];
NSLog(@"arr1 count: %ld", [arr1 count]); //arr1 count: 3
NSObject *obj1;
NSObject *obj2 = [[NSObject alloc] init];
NSObject *obj3 = [NSNull null];
NSObject *obj4 = [NSObject new];
NSArray *arr2 = [NSArray arrayWithObjects:obj1, obj2, obj3, obj4, nil];
NSLog(@"arr2 count: %ld", [arr2 count]); //arr2 count: 0
为啥第一个数组元素有三个,而第二个数组元素为0.先看看:
NSObject* obj;
if (nil == obj) {
NSLog(@"obj is nil");
}
else {
NSLog(@"obj is not nil");
}这个输出:obj is nil。而NSArray是以nil结尾的。所以知道原因了吧!
(2)
//有异常!
NSObject *obj1 = [NSNull null];
NSArray *arr1 = [NSArray arrayWithObjects:@"One", @"TWO", obj1, @"three" ,nil];
for (NSString *str in arr1) {
NSLog(@"array object: %@", [str lowercaseString]);
}
//修改
NSObject *obj1 = [NSNull null];
NSArray *arr1 = [NSArray arrayWithObjects:@"One", @"TWO", obj1, @"three" ,nil];
for (NSString *str in arr1) {
if (![str isEqual:[NSNull null]]){
NSLog(@"array object: %@", [str lowercaseString]);
}
}
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 |
定义某一实例对象为空值。例如:
NSObject* obj = nil; if (nil == obj) { NSLog(@"obj is nil"); } else { NSLog(@"obj is not nil"); }
二、Nil:类为空
定义某一类为空。例如:
Class someClass = Nil; Class anotherClass = [NSString class];
三、NULL:基本数据对象指针为空
用于c语言的各种数据类型的指针为空。例如:
int *pointerToInt = NULL; char *pointerToChar = NULL; struct TreeNode *rootNode = NULL;
四、NSNull
集合对象无法包含 nil 作为其具体值,如NSArray、NSSet和NSDictionary。相应地,nil 值用一个特定的对象 NSNull 来表示。NSNull 提供了一个单一实例用于表示对象属性中的的nil值。
@interface NSNull : NSObject <NSCopying, NSSecureCoding> + (NSNull *)null; @end
在NSNull单例类中,提供了唯一的方法null:Returns the singleton instance of NSNull.
例如:
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary]; mutableDictionary[@"someKey"] = [NSNull null]; // Sets value of NSNull singleton for `someKey` 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)
NSObject *obj1 = [[NSObject alloc] init];
NSObject *obj2 = [NSNull null];
NSObject *obj3 = [NSObject new];
NSObject *obj4;
NSArray *arr1 = [NSArray arrayWithObjects:obj1, obj2, obj3, obj4, nil];
NSLog(@"arr1 count: %ld", [arr1 count]); //arr1 count: 3
NSObject *obj1;
NSObject *obj2 = [[NSObject alloc] init];
NSObject *obj3 = [NSNull null];
NSObject *obj4 = [NSObject new];
NSArray *arr2 = [NSArray arrayWithObjects:obj1, obj2, obj3, obj4, nil];
NSLog(@"arr2 count: %ld", [arr2 count]); //arr2 count: 0
为啥第一个数组元素有三个,而第二个数组元素为0.先看看:
NSObject* obj;
if (nil == obj) {
NSLog(@"obj is nil");
}
else {
NSLog(@"obj is not nil");
}这个输出:obj is nil。而NSArray是以nil结尾的。所以知道原因了吧!
(2)
//有异常!
NSObject *obj1 = [NSNull null];
NSArray *arr1 = [NSArray arrayWithObjects:@"One", @"TWO", obj1, @"three" ,nil];
for (NSString *str in arr1) {
NSLog(@"array object: %@", [str lowercaseString]);
}
//修改
NSObject *obj1 = [NSNull null];
NSArray *arr1 = [NSArray arrayWithObjects:@"One", @"TWO", obj1, @"three" ,nil];
for (NSString *str in arr1) {
if (![str isEqual:[NSNull null]]){
NSLog(@"array object: %@", [str lowercaseString]);
}
}
相关文章推荐
- 基于不要返回null之EmptyFactory的应用详解
- sql 语句中的 NULL值
- JavaScript中为什么null==0为false而null大于=0为true(个人研究)
- JavaScript Undefined,Null类型和NaN值区别
- MS Server和Oracle中对NULL处理的一些细节差异
- 解析C语言中空指针、空指针常量、NULL & 0的详解
- SQL Server、Oracle和MySQL判断NULL的方法
- jquery获取下拉列表的值为null的解决方法
- 区分JS中的undefined,null,"",0和false
- JS基础之undefined与null的区别分析
- 理解Javascript_02_理解undefined和null
- asp.net String.Empty NULL 不同之处
- Javascript 中 null、NaN和undefined的区别总结
- JavaScript中的null和undefined解析
- asp.net中DBNull.Value,null,String.Empty区别浅析
- js中 关于undefined和null的区别介绍
- javascript中的undefined 与 null 的区别 补充篇
- Objective-C 消息传递机制详解
- vm安装mac os x 10.7 lion