nil

  • nil 是 ObjC 对象的字面空值,对应 id 类型的对象,或者使用 @interface 声明的 ObjC 对象。
  • 例如:
    NSString *someString = nil;
    NSURL *someURL = nil;
    id someObject = nil; if (anotherObject == nil) // do something
  • 定义:
    // 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 是 ObjC 类类型的书面空值,对应 Class 类型对象。
  • 例如:
    Class someClass = Nil;
    Class anotherClass = [NSString class];

      

  • 定义声明和 nil 是差不多的,值相同:
    // objc.h
    #ifndef Nil
    # if __has_feature(cxx_nullptr)
    # define Nil nullptr
    # else
    # define Nil __DARWIN_NULL
    # endif
    #endif

NULL

  • NULL 是任意的 C 指针空值。
  • 例如:
    int *pointerToInt = NULL;
    char *pointerToChar = NULL;
    struct TreeNode *rootNode = NULL;
  • 定义:
    // in stddef.h
    #define NULL ((void*)0)

NSNull

  • NSNull 是一个代表空值的类,是一个 ObjC 对象。实际上它只有一个单例方法:+[NSNull null],一般用于表示集合中值为空的对象。
  • 例子说明:
    // 因为 nil 被用来用为集合结束的标志,所以 nil 不能存储在 Foundation 集合里。
    NSArray *array = [NSArray arrayWithObjects:@"one", @"two", nil]; // 错误的使用
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:nil forKey:@"someKey"]; // 正确的使用
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:[NSNull null] forKey:@"someKey"];
  • 定义:
    /*  NSNull.h
    Copyright (c) 1994-2012, Apple Inc. All rights reserved.
    */ #import <Foundation/NSObject.h> @interface NSNull : NSObject <NSCopying, NSSecureCoding> + (NSNull *)null; @end

     

    最后再总结下:

      nil:指向一个对象的空指针

      Nil:指向一个类的空指针

          NULL:指向其他类型(如:基本类型、C类型)的空指针

          NSNull:通常表示集合中的空值

      

最新文章

  1. linux系统下基于mono部署asp.net,使用ef6与mysql出现的问题【索引】
  2. ajax携带状态值
  3. x01.os.12: 在 windows 中写 OS
  4. 疯狂位图之——位图生成12GB无重复随机乱序大整数集
  5. ls -alrth 及ls 详解
  6. 在Dll中使用 TFDQuery 的 LoadFromStream 方法注意问题
  7. 【转】Android学习系列(1)--为App签名(为apk签名)
  8. UVA - 10239 The Book-shelver&amp;#39;s Problem
  9. C语言学习之笔记
  10. WEB前端资源集(一)
  11. 【转载】SDL2.0在mfc窗口中显示yuv的一种方法
  12. Web系统中Mic设备的应用实例
  13. php+Ajax 例子
  14. ②---Java开发工具Eclipse安装配置
  15. 高级组件——菜单栏JMenuBar
  16. 【转】const int *p和int * const p的区别(常量指针与指向常量的指针)
  17. [React] 09 - Tutorial: components
  18. html5-新元素新布局模板-完善中
  19. Lab 3-4
  20. ​ oracle分区表(附带按照月自动分区、按天自动分区)

热门文章

  1. springmvc3 拦截器,过滤ajax请求,判断用户登录,拦截规则设置
  2. UVA - 315
  3. POJ-2778
  4. Linux 基础——ls 命令
  5. 理解JWT(Json Web Token)
  6. WPF Binding 的顺序问题
  7. 【剑指offer】面试题 57. 和为 S 的数字
  8. 京东前端:PhantomJS 和NodeJS在网站前端监控平台的最佳实践
  9. 【转载】CoordinatorLayout源码解析
  10. 如何求先序排列和后序排列——hihocoder1049+洛谷1030+HDU1710+POJ2255+UVA548【二叉树递归搜索】