The difference between nil, nil, NULL, Nsnull

Source: Internet
Author: User
Tags define null

NIL: A null pointer to an object that assigns a null value to the objective C ID object.

Nil: A null pointer to a class that represents a null value for the class.

NULL: A null pointer to another type (for example, base type, C type) to assign a null value to a non-object pointer.

NSNull: An object in the collection object that represents a null value.

1.nil the pointer to an object is null defined 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 *str = 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 Class1 = Nil;Clsss Class2 = [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            

Simple example for assigning null values to non-object pointers

int   *intA    = NULL;char *charC     = NULL;struct structStr = NULL;

4.NSNull is a class in objective-c.

NSNull has + (NSNull *) null; Single-instance method.

Many are 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); // 输出 3,NSArray以nil结尾

//

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:                                @"Object0", @"Key0",                                @"Object1", @"Key1", nil, @"Key-nil" @"Object2", @"Key2", nil];NSLog(@"%@", dictionary); // 输出2个key-value,NSDictionary也是以nil结尾

//

NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];[mutableDictionary setObject:nil forKey:@"Key-nil"]; // 会引起Crash[mutableDictionary setObject:[NSNull null] forKey:@"Key-nil"]; // 不会引起Crash//所以在使用时,如下方法是比较安全的[mutableDictionary setObject:(nil == value ? [NSNull null] : value) forKey:@"Key"];



Wen/jasomzl (author of Jane's book)
Original link: http://www.jianshu.com/p/c01e875686d3
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

The difference between nil, nil, NULL, 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.