I want to know the difference between nil
and [NSNull null]
, so I use NSLog(@"nil==== =%p",nil), and NSLog(@"null====%p",[NSNull null])
to print out their memory addresses, but the result of printing is nil==== =0x0, null=== 0x1022c2f08
, I don't quite understand what 0x0
means, can anyone help me understand?
Asked
Active
Viewed 252 times
0
-
`0x0` is hexadecimal syntax for the decimal value `0` – paddy Oct 26 '18 at 03:06
-
Does it take up memory space? – JackySong Oct 26 '18 at 03:10
-
Possible duplicate of [iphone - \[NSNull getCharacters:\]:](https://stackoverflow.com/questions/34663988/iphone-nsnull-getcharacters) – E.Coms Oct 26 '18 at 03:16
-
That depends. It is a keyword in objective C, which has the same type as a pointer. If you store a `nil` value somewhere then sure, it takes up storage. If you use it in your program, technically that occupies storage in the program segment of memory. But by your apparent understanding of memory, I'm inclined to say the answer is "no". It's certainly not an object that can be instanced, if that's what you mean. – paddy Oct 26 '18 at 03:17
-
Setting a pointer to nil (nothing, 0, null).. means the pointer points to nothing or the address 0x0 aka.. 0.. However.. `[NSNull null]` is an OBJECT that is used to represent nil/null in a dictionary because the dictionary in iOS cannot store "nil" values.. IE: When parsing JSON, iOS stores "NSNull" to represent nil in a dictionary. Think of "NSNull" as the boxed version of the primitive "nil".. `"NSNull - A singleton object used to represent null values in collection objects that don’t allow nil values." - Apple` – Brandon Oct 26 '18 at 03:20
-
@paddy Yeah, I understand. Thank you. – JackySong Oct 26 '18 at 05:54
-
@Brandon I understand. Thank you. – JackySong Oct 26 '18 at 05:54
1 Answers
3
nil
Literal null value for Objective-C objects。it means an Objective-C object pointer that points to 0x0。all objective c pointers start with nil.
NSNull
A singleton object used to represent null object。It is wrapper for null object, so that you can put it in a collection like NSArray/NSDictionary
NSArray * a1 = @[[NSNull null]]; //Good
NSArray * a2 = @[nil]; // Crash

Leo
- 24,596
- 11
- 71
- 92
-
Well, the link content is very detailed, and it makes me understand now, thank you. – JackySong Oct 26 '18 at 05:52