先来看个stackOverflow上的例子:
if (nameTextField.text != (id)[NSNull null] || nameTextField.text.length != 0 ) {
NSString *msg = [[NSString alloc] initWithFormat:@"Hello, %@", txtName.text];
[lblMessage setText:msg];
}
结果:无论nameTextField.text 是否为空,都会进入到if 条件句中。
错误在: nameTextField.text 如果为空,那么它 ==nil 而不是 (id)(NSNull null),所以无论如何,nameTextFiled != (id)[NSNull null] 都是true
我们先来看下这些空值的定义:
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_EXPORT
CFTypeID CFNullGetTypeID(void);
CF_EXPORT
const CFNullRef kCFNull; // the singleton null instance
NSNull *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/