轉(zhuǎn)載在 http://blog.csdn.net/xunyn/article/details/8283653
http://www.reibang.com/p/3aaefb3bcf73
1筝野、nil和null從字面意思來理解比較簡單胖翰,nil是一個(gè)對象还惠,而NULL是一個(gè)值戴而,我的理解為nil是將對象設(shè)置為空石景,而null是將基本類型設(shè)置為空的竖哩,個(gè)人感覺有點(diǎn)像屬性當(dāng)中该抒,基本類型分配為assign NSString類型一般分配copy懂算,而對象一般用retain窿祥。而且我們對于nil調(diào)用方法株憾,不會產(chǎn)生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是一個(gè)對象指針為空贝奇,Nil是一個(gè)類指針為空虹菲,NULL是基本數(shù)據(jù)類型為空。這些可以理解為nil掉瞳,Nil毕源, NULL的區(qū)別吧。
2陕习、NSNULL霎褐,NULL和nil在本質(zhì)上應(yīng)該是一樣的,NULL和nil其實(shí)就是0该镣,但是在Objective-c中冻璃,對于像NSArray這樣的類型,nil或NULL不能做為加到其中的Object损合,如果定義了一個(gè)NSArray省艳,為其分配了內(nèi)存,又想設(shè)置其中的內(nèi)容為空塌忽,則可以用[NSNULL null返回的對對象來初始化NSArray中的內(nèi)容拍埠,我的感覺有點(diǎn)像C語言中malloc一個(gè)內(nèi)存空間,然后用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枣购、一個(gè)可以研究一下的問題在dealloc中-(void) dealloc{self.test = nil; [_test release];test = nil;}這幾個(gè)的區(qū)別先說最簡單的 [_test release]; 這個(gè)就是將引用技術(shù)減1,所謂的引用計(jì)數(shù)就是看看有多個(gè)指針指向一塊內(nèi)存實(shí)體擦耀,當(dāng)release一次棉圈,就是指針減少一個(gè),release到了0的時(shí)候眷蜓,就是真正把這塊內(nèi)存歸還給系統(tǒng)的時(shí)候了再說self.test = nil;說明一下 屬性和setter和getter方法就不難理解了-(void) setTest:(NSString *)newString{if(_test != newString)[_test release];_test = [newString retain];}-(NSString *)test{return _test;}這個(gè)是setter和getter方法分瘾,而在這個(gè)問題中相當(dāng)于剛才的代碼改變?yōu)閕f(_test != nil)[_test release];_test = nil;現(xiàn)在就比較容易解釋了,setter方法會retain nil對象吁系,在這之前已經(jīng)先release了舊的對象德召,這個(gè)方法優(yōu)點(diǎn)是成員變量連指向隨機(jī)數(shù)據(jù)的機(jī)會都沒有,而通過別的方式汽纤,就可能會出現(xiàn)指向隨機(jī)數(shù)據(jù)的情況上岗。當(dāng)release了之后,萬一有別的方法要用要存取它蕴坪,如果它已經(jīng)dealloc了肴掷,可能就會crash敬锐,而指向nil之后,就不會發(fā)生錯(cuò)誤了呆瞻。nil說白了就是計(jì)數(shù)器為0台夺,這么說吧,當(dāng)真正release一個(gè)對象的時(shí)候痴脾,NSLog是打印不了它指向的內(nèi)存控件的颤介,而當(dāng)nil的時(shí)候,是可以打印出來指向的一個(gè)內(nèi)存空間明郭。那么現(xiàn)在也不難解釋test = nil; 單純的這種用法可以說是自己給自己制造內(nèi)存泄露买窟,這里可以這么理解,就是相當(dāng)于將指向?qū)ο蟮闹羔樦苯雍蛯ο笠坏秲蓴嗔耸矶āV苯幼宼est指向nil,而內(nèi)存實(shí)體不會消失瞳购,也不會有系統(tǒng)回收话侄。
先來看個(gè)stackOverflow上的例子:
if (nameTextField.text != (id)[NSNull null] || nameTextField.text.length != 0 ) {
NSString *msg = [[NSString alloc] initWithFormat:@"Hello, %@", txtName.text];
[lblMessage setText:msg];
}
結(jié)果:無論nameTextField.text 是否為空,都會進(jìn)入到if 條件句中学赛。
錯(cuò)誤在: nameTextField.text 如果為空年堆,那么它 ==nil 而不是 (id)(NSNull null),所以無論如何,nameTextFiled != (id)[NSNull null] 都是true
我們先來看下這些空值的定義:
nil: Defines the id of a null instance盏浇,指向一個(gè)(實(shí)例)對象的空指針
例如:
NSString *msg = nil;
NSDate *date =nil;
Nil: Defines the id of a null class变丧,指向一個(gè)類的空指針
例如:
Class class = Nil;
NULL:定義其他類型(基本類型、C類型)的空指針
char *p = NULL;
NSNull:數(shù)組中元素的占位符绢掰,數(shù)據(jù)中的元素不能為nil(可以為空痒蓬,也就是NSNull),
原因:nil 是數(shù)組的結(jié)束標(biāo)志
如果用nil滴劲,就會變成
NSArray *array = [NSArray arrayWithObjects:
[[NSObject alloc] init],
nil,
[[NSObject alloc] init],
[[NSObject alloc] init],
nil];攻晒,
那么數(shù)組到第二個(gè)位置就會結(jié)束。打印[array count]的話會顯示1而不是5
kCFNull: NSNull的單例
CoreFoundation 中有一段對 kCFNull的定義班挖,實(shí)際上就是 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>**
主要的區(qū)別就在 nil 系 和 NSNull系 的區(qū)別
nil : 作為對象的空指針和數(shù)組的結(jié)束標(biāo)志
NSNull:作為數(shù)組中的空值占位符
寫這篇文章我修改了好多次鲁捏,隨著學(xué)習(xí)的深入,有些早期的認(rèn)識是錯(cuò)誤的跋糗健8贰!双揪!
例如這段代碼
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);
結(jié)果全是1动羽,所以這樣看,本質(zhì)上 nil , Nil 和 NULL 是一樣的
-->聰明的你能發(fā)現(xiàn)上面的錯(cuò)誤碼盟榴?曹质??
看下下面這段說明
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.