nil、Nil、NULL与NSNull的区别
2016-07-19 19:44
344 查看
1.nil
指向一个对象的指针为空 在objc.h中的定义如下所示:
#ifndef nil
# if __has_feature(cxx_nullptr)
# define nil nullptr
# else
# define nil __DARWIN_NULL
# endif
#endif
在Objective-C中用于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
在Objective-C中用于Class类型的对象
Class aClass = Nil;
Clsss bClass = [NSURL class];
3.NULL
指向C类型的指针为空 在stddef.h中定义如下:
#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 = NULL;
struct stStruct = NULL;
4.NSNull
在Objective-C中是一个类,只是名字中有个Null,NSNull有 + (NSNull *)null; 单例方法,多用于集合(NSArray,NSDictionary)中值为空的对象
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 *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
@"Object0", @"Key0",
@"Object1", @"Key1",
nil, @"Key-nil"
@"Object2", @"Key2",
nil];
NSLog(@"%@", dictionary); // 输出2个key-value,NSDictionary也是以nil结尾
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"];
指向一个对象的指针为空 在objc.h中的定义如下所示:
#ifndef nil
# if __has_feature(cxx_nullptr)
# define nil nullptr
# else
# define nil __DARWIN_NULL
# endif
#endif
在Objective-C中用于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
在Objective-C中用于Class类型的对象
Class aClass = Nil;
Clsss bClass = [NSURL class];
3.NULL
指向C类型的指针为空 在stddef.h中定义如下:
#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 = NULL;
struct stStruct = NULL;
4.NSNull
在Objective-C中是一个类,只是名字中有个Null,NSNull有 + (NSNull *)null; 单例方法,多用于集合(NSArray,NSDictionary)中值为空的对象
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 *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
@"Object0", @"Key0",
@"Object1", @"Key1",
nil, @"Key-nil"
@"Object2", @"Key2",
nil];
NSLog(@"%@", dictionary); // 输出2个key-value,NSDictionary也是以nil结尾
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"];
相关文章推荐
- iOS中nil、Nil、NULL、NSNull详解
- ios nil、NULL和NSNull 的区别
- null nil Nil [NSNull null] in Swift
- Objective C 中的nil,Nil,NULL和NSNull理解
- 给model添加数据时,自动处理nil以及null数据
- IOS之nil,Nil,null,Null,NULL,NSNull
- Objective C 中的nil,Nil,NULL和NSNull理解
- nil、Nil、NULL、NSNull 的区别
- iOS 中 的nil、Nil、 NULL 和 NSNull
- nil/Nil/NULL/NSNULL区别
- Foundation的一些常用类
- 黑马程序员-----面霸题目
- iOS网络请求下来的数据显示时出现的问题
- nil, Nil, NULL和NSNull的区别
- nil,NULL,NSNull的区别
- [swift、oc]浅聊nil、Nil、NULL、NSNull的区别
- 第七章 集合
- Foundation NSNumber NSValue NSNull NSDate
- IOS中类和对象以及nil/Nil/NULL的区别
- NIL、NIL、NULL和NSNULL区别