Nil, nil, NULL, NSNull的一点区别


Nil, nil, NULL本质都差不多,三个基本上可以通用,定义大概就如下:


#ifndef Nil
# if __has_feature(cxx_nullptr)
#   define Nil nullptr
# else
#   define Nil __DARWIN_NULL
# endif
#endif

#ifndef nil
# if __has_feature(cxx_nullptr)
#   define nil nullptr
# else
#   define nil __DARWIN_NULL
# endif
#endif


#ifdef __cplusplus
#ifdef __GNUG__
#define __DARWIN_NULL __null
#else /* ! __GNUG__ */
#ifdef __LP64__
#define __DARWIN_NULL (0L)
#else /* !__LP64__ */
#define __DARWIN_NULL 0
#endif /* __LP64__ */
#endif /* __GNUG__ */
#else /* ! __cplusplus */
#define __DARWIN_NULL ((void *)0)
#endif /* __cplusplus */

#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif


NSNull在苹果文档中是这样描述的:

The NSNull class defines a singleton object used to represent null values in collection objects (which don’t allow nil values).


也就是说,操作集合类数据类型对象时,需要使用NSNull来判断,而不是仅仅通过对象是否为Nil来判断。有些时候对象非空,但是值是空的(==[NSNull null])。


NSDictionary *dicValue = [NSDictionary dictionaryWithObjectsAndKeys:
                                                @"A", @"B", [NSNull null], @"D", nil, @"E", Nil];
   
    NSString *strValue = [dicValue objectForKey:@"B"];
    if ( Nil != strValue )
    {
        if ( [NSNull null] != (NSNull*)strValue )
        {
            NSLog(@"strValue { %@ }",strValue);
        }
        else
        {
            NSLog(@"strValue NSNull null");
        }
    }
    else
    {
        NSLog(@"strValue Nil");
    }
    
    strValue = [dicValue objectForKey:@"D"];
    if ( Nil != strValue )
    {
        if ( [NSNull null] != (NSNull*)strValue )
        {
            NSLog(@"strValue { %@ }",strValue);
        }
        else
        {
            NSLog(@"strValue NSNull null");
        }
    }
    else
    {
        NSLog(@"strValue Nil");
    }

    // 嘎嘎
    // strValue = [dicValue objectForKey:@"E"];
    strValue = [dicValue objectForKey:@"F"];
    if ( Nil != strValue )
    {
        if ( [NSNull null] != (NSNull*)strValue )
        {
            NSLog(@"strValue { %@ }",strValue);
        }
        else
        {
            NSLog(@"strValue NSNull null");
        }
    }
    else
    {
        NSLog(@"strValue Nil");
    }




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值