nil: A null pointer to an Objective-C object.
( #define nil ((id)0) )
Nil: A null pointer to an Objective-C class.
NULL: A null pointer to anything else, is for C-style memory pointers.
( #define NULL ((void *)0) )
NSNull: A class defines a singleton object used to represent null values in collection objects (which don't allow nil values).
[NSNull null]: The singleton instance of NSNull.
Technically they're all the same,,, but in practice they give someone reading your code some hints about what's going on; just like naming classes with a capital letter and instances with lowercase is recommended, but not required.
If someone sees you passing NULL, they know the receiver expects a C pointer. If they see nil, they know the receiver is expecting an object. If they see Nil, they know the receiver is expecting a class. Readability.
if obj is nil , [obj message] will return NO, without NSException
if obj is NSNull , [obj message will throw a NSException
Demo1:
[NSApp beginSheet:sheet modalForWindow:mainWindow modalDelegate:nil //pointing to an object didEndSelector:NULL //pointing to a non object/class contextInfo:NULL]; //pointing to a non object/class
Demo2:
NSObject *obj1; if (obj1 != nil) { NSLog(@"object is not nil"); }else { NSLog(@"object is nil"); } testClass *c1; if (c1 != Nil) { NSLog(@"class is not Nil"); }else { NSLog(@"class is Nil"); } int *money; if (money != NULL) { NSLog(@"money is not NULL"); }else { NSLog(@"money is NULL"); }
Demo3:
NSObject *obj1 = [[NSObject alloc] init]; NSObject *obj2 = [NSNull null]; NSObject *obj3 = [NSObject new]; NSObject *obj4; NSArray *arr1 = [NSArray arrayWithObjects:obj1, obj2, obj3, obj4, nil]; NSLog(@"arr1 count: %ld", [arr1 count]); //arr1 count: 3 NSObject *obj1; NSObject *obj2 = [[NSObject alloc] init]; NSObject *obj3 = [NSNull null]; NSObject *obj4 = [NSObject new]; NSArray *arr2 = [NSArray arrayWithObjects:obj1, obj2, obj3, obj4, nil]; NSLog(@"arr2 count: %ld", [arr2 count]); //arr2 count: 0
Demo4:
//有异常! NSObject *obj1 = [NSNull null]; NSArray *arr1 = [NSArray arrayWithObjects:@"One", @"TWO", obj1, @"three" ,nil]; for (NSString *str in arr1) { NSLog(@"array object: %@", [str lowercaseString]); } //修改 NSObject *obj1 = [NSNull null]; NSArray *arr1 = [NSArray arrayWithObjects:@"One", @"TWO", obj1, @"three" ,nil]; for (NSString *str in arr1) { if (![str isEqual:[NSNull null]]){ NSLog(@"array object: %@", [str lowercaseString]); } }
相关推荐
相信有不少朋友想知道,在 Objective-C 中 nil 和 Nil 以及 NULL 的区别。最重要的是,在面试中还有不少朋友常会被问到。现在小编在这里统一详细说明。
主要介绍了iOS中nil、Nil、NULL、NSNull详解的相关资料,需要的朋友可以参考下
IOS 基础之nil,NULL,NSNULL区别详解 ① nil:一般赋值给空对象。 ② NULL:NULL 是一个通用指针(泛型指针)。 一般赋值给 nil 之外的其他空值。如SEL等。 ③ NSNULL:[NSNull null] 是一个对象,他用在不能使用 ...
服务器不靠谱? 总是数据崩溃? 试试呗!
1.数组越界,nil值初始化导致的崩溃 2.对字典插nil值,或者读取NSNULL导致的崩溃 3.字符串的截取越界导致的崩溃 4.doesNotRecogniz
IOS 开发之对象为空的判断(nil、null)详解 前言: 在开发中,会遇到很多空的情况,有时候取得对象(null),还有时候会得到的情况,我们需要判断是否为空,进行return; id result; // 针对(null)这种情况 if...
NullSafe是NSNull上的一个简单类别,它为无法识别的消息返回nil而不是引发异常。 这消除了导致崩溃的常见原因,例如,JSON数据包含空值而不是数组或字符串,并且应用程序中的网络代码不希望它。 支持的iOS和SDK版本 ...
下面总结了iOS开发过程中的一些常用宏,会持续的往里面添加。 Objective-C //字符串是否为空 #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO ...
PropertyMapper, Objective C iOS应用程序的属性映射 停止在iOS应用程序中重复你的数据分析代码。数据解析是我们在应用程序中需要做的最...通常的分析需要以下步骤:确保你将NSNull转换为nil而不是崩溃正确处理可选参数
C学习资料汇总专题资源目录:【】Objective-C NSAutoreleasePool【】Objective-c 中 nil, Nil, NULL和NSNull的区别【】Objective-C 中self 和 super【】Objective-C 的编程之道【】Objective-C_2-1.0_Mac和iOS开发 ...
但是并没有考虑到其中存在的一些问题,例如当字符串中存在空格或者换行时或者当请求后台数据时得到的是进行JSON解析的时候, 如果解析出的NSDictionary中某个key对应的value为空, 则系统会把它处理为NSNull类的单例...
在ios应用中,如果从网络请求数据,返回json或者是xml格式的数据时,经常会遇到空串,一般接口是用java等语言写的,如果是安卓,因为源语言都是java,只需判断是否等于null即可,但是在ios中会出现各种各项的形式,...
确保将NSNull转换为nil而不崩溃 优雅地处理可选参数 进行类型转换 数据格式验证 为什么选择属性映射器? 有一些诸如Mantle,RESTKit之类的库可以帮助您解决这些问题,但是我想要的是自包含的,易于更改/删除且需要...
JJException 常见问题不会因JJException崩溃,钩住无法识别的选择器,越界,参数为nil等。将异常引发到接口,然后将异常记录保存到日志中,升级应用程序或Hot-Fix来解决例外。保护App,一般常见的问题不会导致闪退,...