Objective C 中的nil,Nil,NULL跟NSNull理解

Objective C 中的nil,Nil,NULL和NSNull理解

ObjC 里面的几个空值符号经常会差点把我搞死,这些基础的东西一点要弄清楚才行,以提高码农的基本素质。

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 或 NSNil

ObjC 不存在这两个符号!

小结

虽然 nil, Nil, NULL 的值相同,理解它们之间的书面意义才重要,让代码更加明确,增加可读性。

参考资料

  • Difference between nil,NIL and null – Stack Overflow
  • Topics for Cocoa: Using Null.

var:http://magicalboy.com/null-value-in-objective-c/