Explanation of nil, Nil, NULL, and NSNull in iOS, nilnsnull

Source: Internet
Author: User
Tags define null

Explanation of nil, Nil, NULL, and NSNull in iOS, nilnsnull
Nil

  • Nil is the literal null value of the ObjC object, corresponding to the id type object, or the ObjC object declared by @ interface.
  • For example:
    NSString *someString = nil;NSURL *someURL = nil;id someObject = nil; if (anotherObject == nil) // do something
  • Definition:
    // objc.h#ifndef nil# if __has_feature(cxx_nullptr)#   define nil nullptr# else#   define nil __DARWIN_NULL# endif#endif // __DARWIN_NULL in _types.h #define __DARWIN_NULL ((void *)0)
Nil
  • Nil is a written null value of the ObjC Class type, which corresponds to a Class object.
  • For example:
    Class someClass = Nil;Class anotherClass = [NSString class];

      

  • The definition Definition is similar to that of nil, with the same value:
    // objc.h#ifndef Nil# if __has_feature(cxx_nullptr)#   define Nil nullptr# else#   define Nil __DARWIN_NULL# endif#endif
NULL
  • NULL is a NULL value of any C pointer.
  • For example:
    int *pointerToInt = NULL;char *pointerToChar = NULL;struct TreeNode *rootNode = NULL;
  • Definition:
    // in stddef.h#define NULL ((void*)0)
NSNull
  • NSNull is a class that represents null values and is an ObjC object. In fact, it only has one Singleton method: + [NSNull null], which is generally used to indicate objects with null values in the set.
  • Example:
    // Nil cannot be stored in the Foundation set because nil is used to end the set. NSArray * array = [NSArray arrayWithObjects: @ "one", @ "two", nil]; // NSMutableDictionary * dict = [NSMutableDictionary]; [dict setObject: nil forKey: @ "someKey"]; // use NSMutableDictionary correctly * dict = [NSMutableDictionary dictionary]; [dict setObject: [NSNull null] forKey: @ "someKey"];
  • Definition:
    /*  NSNull.h    Copyright (c) 1994-2012, Apple Inc. All rights reserved.*/ #import <Foundation/NSObject.h> @interface NSNull : NSObject <NSCopying, NSSecureCoding> + (NSNull *)null; @end

     

    Summary:

    Nil: NULL pointer to an object

    Nil: NULL pointer to a class

    NULL: NULL pointer to other types (such as basic type and C type)

    NSNull: Null Value in the set.

      

Related Article

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.