区分nil,Nil,Null,NSNull
2015-11-10 15:10
246 查看
1.nil
指向对象的指针为空/** 定义 */ #ifndef nil # if __has_feature(cxx_nullptr) # define nil nullptr # else # define nil __DARWIN_NULL # endif #endif
/** OC中用于id类型的对象*/ NSString *name = nil; NSURL *url = nil; id object = nil;
2.Nil
指向类的指针为空/** 定义 */ #ifndef Nil # if __has_feature(cxx_nullptr) # define Nil nullptr # else # define Nil __DARWIN_NULL # endif #endif
/** OC中用于Class类型的对象 */ Class aClass = Nil; Clsss bClass = [NSURL class];
3.Null
指向C类型的指针为空/** 定义 */ #if defined(__need_NULL) #undef NULL #ifdef __cplusplus # if !defined(__MINGW32__) && !defined(_MSC_VER) # define NULL __null # else # define NULL 0 # endif #else # define NULL ((void*)0) #endif
int *pInt = NULL; char *chChar <span style="white-space:pre"> </span>= NULL; struct stStruct = NULL;
4.NSNull
在OC中是一个类,NSNull有 + (NSNull *)null; 单例方法,多用于集合(NSArray,NSDictionary)中值为空的对象/** NSArray */ NSArray *array = [NSArray arrayWithObjects:[[NSObject alloc] init], [NSNull null], @"aaa", nil, [[NSObject alloc] init], [[NSObject alloc] init], nil]; NSLog(@"%ld", array.count); // 输出3,NSArray以nil结尾 /** NSDictionary */ NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Object0", @"Key0", @"Object1", @"Key1", nil, @"Key-nil", @"Object2", @"Key2", nil]; NSLog(@"%@", dictionary); // 输出2个key-value,NSDictionary也是以nil结尾 /** NSMutableDictionary */ NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init]; [mutableDictionary setObject:nil forKey:@"Key-nil"]; // 会引起Crash [mutableDictionary setObject:[NSNull null] forKey:@"Key-nil"]; // 不会引起Crash // 一般为了安全,采用如下方法: [mutableDictionary setObject:(nil == value ? [NSNull null] : value) forKey:@"Key"];
相关文章推荐
- Ajax&JSON的简单使用
- Mysql常用status
- sql cookbook笔录(二)
- A System for Rapid, Automatic Shader Level-of-Detail
- [LeetCode] Range Sum Query - Immutable
- ubuntu与windows的文件夹共享
- django模型中manytomanyfield的含义
- SpringMVC原理解析
- 回调使用记录
- Android ThreadLocal类浅析
- 高吞吐量系统设计
- WEKA使用教程(界面工具的用法)
- Android系统移植与调试之------->增加一个双击物理按键打开和关闭闪光灯并将闪光灯状态同步到下拉菜单中
- 黑马程序员_ARC
- 10.1 图标、鼠标指针、字符串和自定义资源
- 03(maven+SSH)网上商城项目实战之数据库设计(PMD)
- 将CFormView嵌入到CDockablePane中
- 黑马程序员——异常
- Linux写配置HDF5的python包h5py
- C__ Linux C 中__user的含义