Objective-c nil, Nil, NULL和NSNull的區別
在OC中可能經常會遇到 nil,Nil,NULL和NSNull,下面分析一下之間的區別:
Symbol | Value | Meaning |
---|---|---|
NULL | (void *)0 | literal null value for C pointers |
nil | (id)0 | literal null value for Objective-C objects |
Nil | (Class)0 | literal null value for Objective-C classes |
NSNull | [NSNull null] | singleton object used to represent null |
一、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語言的各種資料型別的指標為空。例如:
int *pointerToInt = NULL;
char *pointerToChar = NULL;
struct TreeNode *rootNode = NULL;
四、NSNull
集合物件無法包含 nil 作為其具體值,如NSArray、NSSet和NSDictionary。相應地,nil 值用一個特定的物件 NSNull 來表示。NSNull 提供了一個單一例項用於表示物件屬性中的的nil值。
@interface NSNull : NSObject <NSCopying, NSSecureCoding>
+ (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];
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];
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]);
}
}
相關文章
- Objective-C 中 NULL、nil、Nil、NSNull 的定義及不同ObjectNull
- golang nil 切片和空切片區別Golang
- scala中的option[T]、Any、Nothing、Null和NilNull
- Scala中None, Nil, Nothing的區別None
- 自定義錯誤型別時應該注意的 nil !=nil型別
- Go 介面:nil介面為什麼不等於nil?Go
- GO 空指標和nilGo指標
- Go 語言 nil 和介面Go
- 關於零值和nil
- Go 語言中,有時 nil 並不是一個 nilGo
- Go“一個包含nil指標的介面不是nil介面”踩坑Go指標
- MySQL中is not null和!=null和<>null的區別MySqlNull
- attempt to index local ‘result‘ (a nil value)Index
- Swift - 11 - nil聚合運算Swift
- .nil? .empty? .blank? .present? in Ruby on RailsAI
- 由Nil-Targeted Actions說起
- null 和 undefined 的區別NullUndefined
- null 和 undefined 的區別!NullUndefined
- NULL和0的區別Null
- go的 err!=nil 和 panic+recover 這兩種錯誤處理機制的關係和區別是什麼?Go
- Gopher們寫if err != nil是否膩了?Go
- Bunder: What does :require => nil in Gemfile mean?UI
- undefined 和 null 區別?UndefinedNull
- null和undefined區別NullUndefined
- iOS給字典中插入nil的幾種情景iOS
- Swift中 Nil Coalescing 運算子的使用技巧Swift
- (*Type)(nil)有什麼特殊含義嗎?
- JS 的型別(null 和 undefined 的區別)JS型別NullUndefined
- 2>/dev/null和>/dev/null 2>&1和2>&1>/dev/null的區別devNull
- javascrit中undefined和null的區別JavaUndefinedNull
- Object.create(null) 和 {} 的區別ObjectNull
- js中null和undefined的區別JSNullUndefined
- js中undefined和null的區別JSUndefinedNull
- /dev/zero和/dev/null的區別devNull
- Swift 小心字典Value等於nil(容易出錯)Swift
- lisp 習題 (member '(a) '((a) (b))) 為什麼返回nilLisp
- go 如何優雅的判斷變數是否為 nilGo變數
- Java中空串和null串的區別JavaNull