Nil is used to assign values to an object (any object in objective-C belongs to the ID type). null is used to assign values to any pointer. null and nil cannot be exchanged, nil is used to assign values to class pointers (in objective-C, a class is an object and an instance of class meta-class), while nsnull is used for set operations, although they indicate null values, however, the usage is completely different.
Example:
- Id object = nil;
- // The Judgment object is not empty.
- If (object ){
- }
- // The Judgment object is empty.
- If (Object = nil ){
- }
- // Array initialization. The Null Value ends.
- Nsarray * array = [[nsarray alloc] initwithobjects: @ "first", @ "second", nil];
- // Determine whether the array element is empty
- Nsstring * element = [array objectatindex: 2];
- If (nsnull *) element = [nsnull null]) {
- }
- // Determine whether the element of the dictionary object is null
- Nsdictionary * dictionary = [nsdictionary dictionarywithobjectsandkeys:
- @ "IPhone", @ "first", @ "iPad", @ "second", nil];
- Nsstring * value = [dictionary objectforkey: @ "first"];
- If (nsnull *) value = [nsnull null]) {
- }