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

本文详细解释了在Objective-C中Nil、nil与NULL的定义及其区别,并重点介绍了NSNull类的作用,即在集合对象中表示null值。文章还通过一个NSDictionary实例展示了如何正确判断对象是否为NSNull。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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");
    }




### 将 `nil` 值转换为适用于 HTML5 的表示形式 在 Objective-C 中,`nil` 表示空指针,而容器类(如 NSArray 或 NSDictionary)无法直接存储 `nil` 值。为了使这些值能够被安全地传递到 H5 页面并正确解析,在实际开发中通常会使用 `NSNull` 来替代 `nil`[^4]。 当需要将数据从原生应用传递至 H5 页面时,可以通过以下方式处理: #### 数据准备阶段 如果某些字段可能为空,则应将其替换为 `NSNull` 实例后再放入字典或数组中。这样可以防止因尝试存储 `nil` 导致程序崩溃。例如: ```objc NSMutableDictionary *dataDict = [NSMutableDictionary dictionary]; if (!someValue) { dataDict[@"key"] = [NSNull null]; // 使用 NSNull 替代 nil } else { dataDict[@"key"] = someValue; } [_jsBridge callHandler:@"getInfo" data:dataDict]; ``` #### 在 JavaScript 端接收数据 JavaScript 并不存在类似于 `NSNull` 的概念,因此接收到的数据如果是 `[object Object]` 类型且其内部属性均为 `null`,则可以直接判断该值代表的是空值。以下是具体的实现逻辑: 假设通过 `_jsBridge.callHandler:` 方法传递了一个包含潜在 `NSNull` 的 JSON 对象给 H5 页面,那么可以在前端做如下校验: ```javascript function handleData(data) { const queue = data.queue; if (queue === null || typeof queue === 'undefined') { console.log('Received a null or undefined value'); return; } console.log(`Queue Value: ${queue}`); } // Example usage when receiving the message from native side. window.getCallbackFromNativeApp = function(nativeData) { handleData(JSON.parse(nativeData)); }; ``` 上述代码片段展示了如何检测来自 Native 层次的消息中的 `null` 和未定义情况,并采取适当措施加以应对。 另外需要注意的一点是在 iOS WebKit API 下可能会遇到编码解码过程中产生的异常字符问题[^5]。此时建议始终访问具体键名而非整个 body 字符串本身以规避乱码风险。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值