iOS中nil、Nil、NULL、NSNull详解
2024-08-29 17:24:47
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:通常表示集合中的空值
最新文章
- linux系统下基于mono部署asp.net,使用ef6与mysql出现的问题【索引】
- ajax携带状态值
- x01.os.12: 在 windows 中写 OS
- 疯狂位图之——位图生成12GB无重复随机乱序大整数集
- ls -alrth 及ls 详解
- 在Dll中使用 TFDQuery 的 LoadFromStream 方法注意问题
- 【转】Android学习系列(1)--为App签名(为apk签名)
- UVA - 10239 The Book-shelver&;#39;s Problem
- C语言学习之笔记
- WEB前端资源集(一)
- 【转载】SDL2.0在mfc窗口中显示yuv的一种方法
- Web系统中Mic设备的应用实例
- php+Ajax 例子
- ②---Java开发工具Eclipse安装配置
- 高级组件——菜单栏JMenuBar
- 【转】const int *p和int * const p的区别(常量指针与指向常量的指针)
- [React] 09 - Tutorial: components
- html5-新元素新布局模板-完善中
- Lab 3-4
- ​ oracle分区表(附带按照月自动分区、按天自动分区)
热门文章
- springmvc3 拦截器,过滤ajax请求,判断用户登录,拦截规则设置
- UVA - 315
- POJ-2778
- Linux 基础——ls 命令
- 理解JWT(Json Web Token)
- WPF Binding 的顺序问题
- 【剑指offer】面试题 57. 和为 S 的数字
- 京东前端:PhantomJS 和NodeJS在网站前端监控平台的最佳实践
- 【转载】CoordinatorLayout源码解析
- 如何求先序排列和后序排列——hihocoder1049+洛谷1030+HDU1710+POJ2255+UVA548【二叉树递归搜索】