zoukankan      html  css  js  c++  java
  • nil/Nil/NULL/NSNull的区别

    平时开发过程中经常遇到这几个表示空的关键字:nil、Nil、NULL,一向搞不清楚,作为一个有两年开发经验的程序员,连那么基础的东西都不知道未免太丢人了。
    首先要说明的是,nil、Nil、NULL三个关键字和NSNull类都是表示空,只是用处不一样,具体的区别如下:

    一、NULL

    1、声明位置

    stddef.h文件

    2、定义

    [objc] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #undef NULL  
    2. #ifdef __cplusplus  
    3. #  if !defined(__MINGW32__) && !defined(_MSC_VER)  
    4. #    define NULL __null  
    5. #  else  
    6. #    define NULL 0  
    7. #  endif  
    8. #else  
    9. #  define NULL ((void*)0)  
    10. #endif  
    其中__cplusplus表示是不是C++代码,所以对于普通的iOS开发者来说,通常NULL的定义就是:
    [objc] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #  define NULL ((void*)0)  

    因此,NULL本质上是:(void*)0

    3、用处及含义

    NULL表示C指针中的空值

    4、示例

    [objc] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. charchar *string = NULL;  
     

    二、nil

    1、声明位置

    objc.h文件

    2、定义

    [objc] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #ifndef nil  
    2. # if __has_feature(cxx_nullptr)  
    3. #   define nil nullptr  
    4. # else  
    5. #   define nil __DARWIN_NULL  
    6. # endif  
    7. #endif  
    其中__has_feature(cxx_nullptr)用于判断C++中是否有nullptr特性,对于普通iOS开发者来说,nil的定义形式为:
    [objc] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #   define nil __DARWIN_NULL  
    就是说nil最终是__DARWIN_NULL的宏定义,__DARWIN_NULL是定义在_types.h中的宏,其定义形式如下:
    [objc] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #ifdef __cplusplus  
    2. #ifdef __GNUG__  
    3. #define __DARWIN_NULL __null  
    4. #else /* ! __GNUG__ */  
    5. #ifdef __LP64__  
    6. #define __DARWIN_NULL (0L)  
    7. #else /* !__LP64__ */  
    8. #define __DARWIN_NULL 0  
    9. #endif /* __LP64__ */  
    10. #endif /* __GNUG__ */  
    11. #else /* ! __cplusplus */  
    12. #define __DARWIN_NULL ((void *)0)  
    13. #endif /* __cplusplus */  
    非C++代码的__DARWIN_NULL最终定义形式如下:
    [objc] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #define __DARWIN_NULL ((void *)0)  
    也就是说,nil本质上是:(void *)0

    3、用处及含义

    用于表示指向Objective-C中对象的指针的值为空

    4、示例

    [objc] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. NSString *string = nil;  
    2. id anyObject = nil;  
     

    三、Nil

    1、声明位置

    objc.h文件

    2、定义

    [objc] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #ifndef Nil  
    2. # if __has_feature(cxx_nullptr)  
    3. #   define Nil nullptr  
    4. # else  
    5. #   define Nil __DARWIN_NULL  
    6. # endif  
    7. #endif  
    和上面讲到的nil一样,Nil本质上也是:(void *)0

    3、用处及含义

    用于表示Objective-C类(Class)类型的变量值为空

    4、示例

    [objc] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. Class anyClass = Nil;  
     

    四、NSNull

    1、声明位置

    NSNull.h文件

    2、定义

    [objc] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. @interface NSNull : NSObject <NSCopying, NSSecureCoding>  
    2.   
    3. + (NSNull *)null;  
    4.   
    5. @end  

    3、用处及含义

    从定义中可以看出,NSNull是一个Objective-C类,只不过这个类相当特殊,因为它表示的是空值,即什么都不存。它也只有一个单例方法+[NSUll null]。该类通常用于在集合对象中保存一个空的占位对象。

    4、示例

    我们通常初始化NSArray对象的形式如下:
    [objc] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. NSArray *arr = [NSArray arrayWithObjects:@"wang",@"zz",nil];  
    当NSArray里遇到nil时,就说明这个数组对象的元素截止了,即NSArray只关注nil之前的对象,nil之后的对象会被抛弃。比如下面的写法:
    [objc] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. NSArray *arr = [NSArray arrayWithObjects:@"wang",@"zz",nil,@"foogry"];  
    这是NSArray中只会保存wang和zz两个字符串,foogry字符串会被抛弃。
    这种情况,就可以使用NSNull实现:
    [objc] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. NSArray *arr = [NSArray arrayWithObjects:@"wang",@"zz",[NSNull null],@"foogry"];  

    五、总结

    从前面的介绍可以看出,不管是NULL、nil还是Nil,它们本质上都是一样的,都是(void *)0,只是写法不同。这样做的意义是为了区分不同的数据类型,比如你一看到用到了NULL就知道这是个C指针,看到nil就知道这是个Objective-C对象,看到Nil就知道这是个Class类型的数据。

  • 相关阅读:
    leetcode 350. Intersection of Two Arrays II
    leetcode 278. First Bad Version
    leetcode 34. Find First and Last Position of Element in Sorted Array
    leetcode 54. Spiral Matrix
    leetcode 59. Spiral Matrix II
    leetcode 44. Wildcard Matching
    leetcode 10. Regular Expression Matching(正则表达式匹配)
    leetcode 174. Dungeon Game (地下城游戏)
    leetcode 36. Valid Sudoku
    Angular Elements
  • 原文地址:https://www.cnblogs.com/pandas/p/4251867.html
Copyright © 2011-2022 走看看