轉載在 http://blog.csdn.net/xunyn/article/details/8283653
http://www.lxweimin.com/p/3aaefb3bcf73
1、nil和null從字面意思來理解比較簡單,nil是一個對象,而NULL是一個值,我的理解為nil是將對象設置為空,而null是將基本類型設置為空的,個人感覺有點像屬性當中,基本類型分配為assign NSString類型一般分配copy,而對象一般用retain。而且我們對于nil調用方法,不會產生crash或者拋出異常。看一段nil -> Null-pointer to objective- c object
NIL -> Null-pointer to objective- c class
null-> null pointer to primitive type or absence of data.
看一下用法
NSURL *url = nil;
Class class = Nil;
int *pointerInt = NULL;
nil是一個對象指針為空,Nil是一個類指針為空,NULL是基本數據類型為空。這些可以理解為nil,Nil, NULL的區別吧。
2、NSNULL,NULL和nil在本質上應該是一樣的,NULL和nil其實就是0,但是在Objective-c中,對于像NSArray這樣的類型,nil或NULL不能做為加到其中的Object,如果定義了一個NSArray,為其分配了內存,又想設置其中的內容為空,則可以用[NSNULL null返回的對對象來初始化NSArray中的內容,我的感覺有點像C語言中malloc一個內存空間,然后用memset初始化這段空間里的值為0。
_viewControllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < _pages; i++) {
[_viewControllers addObject:[NSNull null]];
}
ymBaseController *controller = [_viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
...
}
[_viewControllers replaceObjectAtIndex:page withObject:controller];
3、一個可以研究一下的問題在dealloc中-(void) dealloc{self.test = nil; [_test release];test = nil;}這幾個的區別先說最簡單的 [_test release]; 這個就是將引用技術減1,所謂的引用計數就是看看有多個指針指向一塊內存實體,當release一次,就是指針減少一個,release到了0的時候,就是真正把這塊內存歸還給系統的時候了再說self.test = nil;說明一下 屬性和setter和getter方法就不難理解了-(void) setTest:(NSString *)newString{if(_test != newString)[_test release];_test = [newString retain];}-(NSString *)test{return _test;}這個是setter和getter方法,而在這個問題中相當于剛才的代碼改變為if(_test != nil)[_test release];_test = nil;現在就比較容易解釋了,setter方法會retain nil對象,在這之前已經先release了舊的對象,這個方法優點是成員變量連指向隨機數據的機會都沒有,而通過別的方式,就可能會出現指向隨機數據的情況。當release了之后,萬一有別的方法要用要存取它,如果它已經dealloc了,可能就會crash,而指向nil之后,就不會發生錯誤了。nil說白了就是計數器為0,這么說吧,當真正release一個對象的時候,NSLog是打印不了它指向的內存控件的,而當nil的時候,是可以打印出來指向的一個內存空間。那么現在也不難解釋test = nil; 單純的這種用法可以說是自己給自己制造內存泄露,這里可以這么理解,就是相當于將指向對象的指針直接和對象一刀兩斷了。直接讓test指向nil,而內存實體不會消失,也不會有系統回收。
先來看個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.