The difference between nil, nil, null and Nsnull

Source: Internet
Author: User
Tags define null

1.nil pointer to an object is empty the definition in objc.h is as follows:
#ifndef nil# if __has_feature (cxx_nullptr) #   define nil nullptr# else#   define nil __darwin_null# endif#endif
Object in objective-c for ID type
NSString *name = nil; Nsurl    *url  = nil;id object      = nil;
2.Nil pointers to a class are null defined as follows:
#ifndef nil# if __has_feature (cxx_nullptr) #   define nil nullptr# else#   define Nil __darwin_null# endif#endif
Objects for class type in Objective-c
Class aclass = Nil; Clsss Bclass = [Nsurl class];
3.NULL pointer to type C is empty as defined in Stddef.h:
#if defined (__need_null) #undef null#ifdef __cplusplus#  if!defined (__mingw32__) &&!defined (_msc_ver) #    define NULL __null#  else#    define NULL 0#  endif#else#  define NULL ((void*) 0) #endif
This is used for the following examples:
int   *pint     
4.NSNull is a class in objective-c, but there is a null,nsnull in the name+ (NSNull*) null; A singleton method that is used for objects with null values in the collection (nsarray,nsdictionary)
Nsarray *array = [Nsarray arraywithobjects:                      [[NSObject alloc] init],                      [NSNull null],                      @ "AAA",                      Nil,                      [[NSObject alloc] init],                      [[NSObject alloc] init], nil]; NSLog (@ "%ld", Array.count); Output 3,nsarray ends with nil
Nsdictionary *dictionary = [[Nsdictionary alloc] Initwithobjectsandkeys:                                @ "Object0", @ "Key0",                                @ "Object1", @ " Key1 ",                                Nil,        @" Key-nil "                                @" Object2 ", @" Key2 ",                                Nil]; NSLog (@ "%@", dictionary); Output of 2 Key-value,nsdictionary is also terminated with nil
Nsmutabledictionary *mutabledictionary = [[Nsmutabledictionary alloc] init]; [Mutabledictionary setobject:nil forkey:@ "Key-nil"]; will cause crash[mutabledictionary setobject:[nsnull null] forkey:@ "Key-nil"]; does not cause crash
So in use, the following methods are relatively safe
[Mutabledictionary setobject: (nil = = value?) [NSNull NULL]: value)                      forkey:@ "Key"];






The difference between nil, nil, null and Nsnull

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.