Objective-c nil, Nil, NULL和NSNull的區別

在OC中可能經常會遇到 nil,Nil,NULL和NSNull,下面分析一下之間的區別:

一、nil:對象為空定義某一實例對象為空值。例如:

NSObject* obj = nil;? ? ? ? ??

if (nil == obj) {? ? ? ? ? ? ??

? ? ? ? ?NSLog(@"obj is nil");? ? ? ? ??

} else {? ? ? ? ? ? ??

? ? ? ? NSLog(@"obj is not nil");? ? ? ? ??

}?

?二、Nil:類為空定義某一類為空。例如:Class someClass = Nil;? ? ? ? ? Class anotherClass = [NSString class];? 三、NULL:基本數據對象指針為空用于c語言的各種數據類型的指針為空。例如:intint *pointerToInt = NULL;? charchar *pointerToChar = NULL;? struct TreeNode *rootNode = NULL;? 四、NSNull集合對象無法包含 nil 作為其具體值,如NSArray、NSSet和NSDictionary。相應地,nil 值用一個特定的對象 NSNull 來表示。NSNull 提供了一個單一實例用于表示對象屬性中的的nil值。@interface NSNull : NSObject+ (NSNull *)null;

@end

在NSNull單例類中,提供了唯一的方法null:Returns the singleton instance of NSNull.

例如:

NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];

mutableDictionary[@"someKey"] = [NSNull null]; // Sets value of NSNull singleton for `someKey`

NSLog(@"Keys: %@", [mutableDictionary allKeys]); // @[@"someKey"]

五、說明:

Technically they're all the same, but in practice they give someone reading your code some hints about what's going on; just like naming classes with a capital letter and instances with lowercase is recommended, but not required.

If someone sees you passing NULL, they know the receiver expects a C pointer. If they see nil, they know the receiver is expecting an object. If they see Nil, they know the receiver is expecting a class. Readability.

六、注

下面附帶幾個有趣的例子:

(1)

NSObject *obj1 = [[NSObject alloc] init];

NSObject *obj2 = [NSNull null];

NSObject *obj3 = [NSObject new];

NSObject *obj4;

NSArray *arr1 = [NSArray arrayWithObjects:obj1, obj2, obj3, obj4, nil nil];

NSLog(@"arr1 count: %ld", [arr1 count]);? ? //arr1 count: 3

NSObject *obj1;

NSObject *obj2 = [[NSObject alloc] init];

NSObject *obj3 = [NSNull null];

NSObject *obj4 = [NSObject new];

NSArray *arr2 = [NSArray arrayWithObjects:obj1, obj2, obj3, obj4, nil nil];

NSLog(@"arr2 count: %ld", [arr2 count]);? //arr2 count: 0

為啥第一個數組元素有三個,而第二個數組元素為0.先看看:

NSObject* obj;

if (nil == obj) {

NSLog(@"obj is nil");

}

else {

NSLog(@"obj is not nil");

}

這個輸出:obj is nil。而NSArray是以nil結尾的。所以知道原因了吧!

(2)

//有異常!

NSObject *obj1 = [NSNull null];

NSArray *arr1 = [NSArray arrayWithObjects:@"One", @"TWO", obj1, @"three" ,nil];

for (NSString *str in arr1) {

NSLog(@"array object: %@", [str lowercaseString]);

}

//修改

NSObject *obj1 = [NSNull null];

NSArray *arr1 = [NSArray arrayWithObjects:@"One", @"TWO", obj1, @"three" ,nil];

for (NSString *str in arr1) {

? ? ? ? ? ? ?if (![str isEqual:[NSNull null]]){

? ? ? ? ? ?NSLog(@"array object: %@", [str lowercaseString]);

}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 類與對象的概念類是對同一類事物高度的抽象,類中定義了這一類對象所應具有的靜態屬性(屬性)和動態屬性(方法)。對象是...
    LZM輪回閱讀 904評論 0 0
  • nil:指向oc中對象的空指針,針對對象。 Nil:指向oc中類的空指針,針對類。 NULL:指向其他類型的空指針...
    DestinyFighter_閱讀 678評論 0 2
  • nil和NULL從字面意思來理解比較簡單,nil是一個對象,而NULL是一個值,我的理解為nil是將對象設置為空,...
    魚來魚往0709閱讀 535評論 0 1
  • 目錄: 在字符串查看指定字符串 UILabel自適應 服務器數據處理 copy解釋 對象及可變字典賦值取值方法 n...
    Ryan___閱讀 753評論 0 0
  • 大綱 26.Protocol-協議 27.delegate-代理 28.Foundation框架介紹 29.NSS...
    天天想念閱讀 1,375評論 0 2