IOS 空值 nil Nil NULL NSNull kCFNull

来源:互联网 发布:deepin linux使用教程 编辑:程序博客网 时间:2024/06/04 00:37
nil:  Defines the id of a null instance,指向一个(实例)对象的空指针 例如:NSString *msg = nil;NSDate *date =nil;
Nil: Defines the id of a null class,指向一个类的空指针例如:Class class = Nil;
NULL:定义其他类型(基本类型、C类型)的空指针char *p = NULL;
NSNull:数组中元素的占位符,数据中的元素不能为nil(可以为空,也就是NSNull),原因:nil 是数组的结束标志如果用nil,就会变成NSArray *array = [NSArray arrayWithObjects:[[NSObject alloc] init], nil,[[NSObject alloc] init], [[NSObject alloc] init], nil];,那么数组到第二个位置就会结束。打印[array count]的话会显示1而不是5
kCFNull: NSNull的单例CoreFoundation 中有一段对 kCFNull的定义,实际上就是 NSNull 的单例typedef const struct CF_BRIDGED_TYPE(NSNull) __CFNull * CFNullRef;CF_EXPORTCFTypeID CFNullGetTypeID(void);CF_EXPORTconst CFNullRef kCFNull; // the singleton null instanceNSNull *null1 = (id)kCFNull;NSNull *null2 = [NSNull null];

Talking is cheap ,show me the code!

NSLog(@"nil is %p",nil);NSLog(@"Nil is %p",Nil);NSLog(@"Null is %p",NULL);NSLog(@"nil is %@",nil);NSLog(@"NSNULL is  %@",kCFNull);nil is 0x0**Nil is 0x0**Null is 0x0**nil is (null)**NSNULL is  <null>**

主要的区别就在 nil 系 和 NSNull系 的区别

nil : 作为对象的空指针和数组的结束标志
NSNull:作为数组中的空值占位符


写这篇文章我修改了好多次,随着学习的深入,有些早期的认识是错误的啊!!!
例如这段代码

    Class class = nil;    Class class1 = Nil;    char *p =nil;    char *p1 = NULL;    NSString *str = NULL;    NSLog(@"nil is : %d",class==nil);    NSLog(@"nil is : %d",class==Nil);    NSLog(@"Nil is : %d",class1==nil);    NSLog(@"Nil is : %d",class1==Nil);    NSLog(@"integer is nil : %d",num == nil);    NSLog(@"integer is NULL : %d",num == NULL);    NSLog(@"integer is Nil : %d",num == Nil);    NSLog(@"nil equals Nil: %d",nil == Nil);    NSLog(@"Nil equals NULL:%d",p == p1);    NSLog(@"nil equals NULL: %d",nil == NULL);结果全是1,所以这样看,本质上 nil , Nil 和 NULL 是一样的-->聪明的你能发现上面的错误码???看下下面这段说明In Objective-C, it's important that you distinguish between objects and primitive types.An **object** is always stored as a pointer, which is the object's location in memory. A pointer is just a number. With NSLog, you can use %p to see this value. You can display it in the debugger too, like this: print myObject. A pointer is displayed as a hexadecimal number, with a 0x prefix. nil is essentially location zero (0x0000). When you allocate any kind of object, you'll get a pointer which isn't zero. When you assign an object to a variable, you are simply copying the memory address, not duplicating the object. With NSLog, you can use %@ to print out an object's description. In the debugger, like this: print-object myObject.**Primitive types** like NSInteger aren't objects. Instead of storing a pointer, usually you just store the value. When you assign an NSInteger variable, you make a copy of the value. You can see the value in the debugger using print. Or like this: NSLog("%ld", (long)currentRow). When you assign a primitive, you copy its value. Don't use %@ or print-object with primitives — they expect objects.

参考:
1.http://blog.sina.com.cn/s/blog_5fb39f910101akm1.html
2.http://xiongzenghuidegithub.github.io/blog/2014/02/23/nil-nsnull-null-kcfnull/

0 0
原创粉丝点击