1.nil
>Defines the id of a null instance.
定義一個(gè)實(shí)例為空, 指向oc中對(duì)象的空指針.
>示例代碼:
NSString *someString = nil;
NSURL *someURL = nil;
id someObject = nil;
if (anotherObject == nil) // do something
>當(dāng)對(duì)某個(gè)對(duì)象release 的同時(shí)最好把他們賦值為nil,這樣可以確保安全性,如果不賦值nil,可能導(dǎo)致程序崩潰.
NSArray * array = [NSArray arrayWithObjects:@"test",@"test1" ,nil];
[array release];
if (array)
{
//僅僅對(duì)數(shù)組release,并沒(méi)有賦值為nil,在程序某個(gè)地方如果繼續(xù)對(duì)數(shù)組操作,程序直接崩潰
NSString * string = [array objectAtIndex:0];
NSLog(@"%@",string);
}
2.NULL
>These macros define null values for classes and instances.
NU
#define __DARWIN_NULL #define__DARWIN_NULLConstants
>示例代碼:
int *pointerToInt = NULL;
char *pointerToChar = NULL;
struct TreeNode *rootNode = NULL;
>在Objective-C里,nil對(duì)象被設(shè)計(jì)來(lái)跟NULL空指針關(guān)聯(lián)的。他們的區(qū)別就是nil是一個(gè)對(duì)象,而NULL只是一個(gè)值。而且我們對(duì)于nil調(diào)用方法,不會(huì)產(chǎn)生crash或者拋出異常。
3.Nil
>Defines the id of a null class.
定義一個(gè)空的類
Available in Mac OS X v10.0 through Mac OS X v10.4.
Declared in NSObjCRuntime.h.
Declared Inobjc.h
>示例代碼:
Class someClass = Nil;
Class anotherClass = [NSString class];
4.NSNull
>The NSNull class defines a singleton object used to represent null values in collection objects (which don’t allow nil values).
NSNull類定義了一個(gè)單例對(duì)象用于表示集合對(duì)象的空值
>集合對(duì)象無(wú)法包含nil作為其具體值,如NSArray、NSSet和NSDictionary。相應(yīng)地,nil值用一個(gè)特定的對(duì)象NSNull來(lái)表示。NSNull提供了一個(gè)單一實(shí)例用于表示對(duì)象屬性中的的nil值。默認(rèn)的實(shí)現(xiàn)方法中,dictionaryWithValuesForKeys:和setValuesForKeysWithDictionary:自動(dòng)地將NSNull和nil相互轉(zhuǎn)換,因此您的對(duì)象不需要進(jìn)行NSNull的測(cè)試操作。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注