nil / Nil / NULL / NSNull
来源:互联网 发布:武清网络推广 编辑:程序博客网 时间:2024/04/18 08:13
http://nshipster.com/nil/
Understanding the concept of nothingness is as much a philosophical issue as it is a pragmatic one. We are inhabitants of a universe of somethings, yet reason in a logical universe of existential uncertainties. As a physical manifestation of a logical system, computers are faced with the intractable problem of how to represent nothing with something.
In Objective-C, there are several different varieties of nothing. The reason for this goes back to a common NSHipster refrain, of how Objective-C bridges the procedural paradigm of C with Smalltalk-inspired object-oriented paradigm.
C represents nothing as 0
for primitive values, and NULL
for pointers (which is equivalent to 0
in a pointer context).
Objective-C builds on C's representation of nothing by adding nil
. nil
is an object pointer to nothing. Although semantically distinct from NULL
, they are technically equivalent to one another.
On the framework level, Foundation defines NSNull
, which defines a class method, +null
, which returns the singleton NSNull
object. NSNull
is different from nil
or NULL
, in that it is an actual object, rather than a zero value.
Additionally, in Foundation/NSObjCRuntime.h, Nil
is defined as a class pointer to nothing. This lesser-known title-case cousin of nil
doesn't show up much very often, but it's at least worth noting.
There's Something About nil
Newly-alloc
'd NSObject
s start life with their contents set to 0
. This means that all pointers that object has to other objects begin as nil
, so it's unnecessary to, for instance, set self.(association) = nil
in init
methods.
Perhaps the most notable behavior of nil
, though, is that it can have messages sent to it.
In other languages, like C++, this would crash your program, but in Objective-C, invoking a method on nil
returns a zero value. This greatly simplifies expressions, as it obviates the need to check fornil
before doing anything:
// For example, this expression...if (name != nil && [name isEqualToString:@"Steve"]) { ... }// ...can be simplified to:if ([name isEqualToString:@"steve"]) { ... }
Being aware of how nil
works in Objective-C allows this convenience to be a feature, and not a lurking bug in your application. Make sure to guard against cases where nil
values are unwanted, either by checking and returning early to fail silently, or adding a NSParameterAssert
to throw an exception.
NSNull
: Something for Nothing
NSNull
is used throughout Foundation and other frameworks to skirt around the limitations of collections like NSArray
and NSDictionary
not being able to contain nil
values. You can think of NSNull
as effectively boxing the NULL
or nil
value so that it can be used in collections:
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];mutableDictionary[@"someKey"] = [NSNull null]; // Sets value of NSNull singleton for `someKey`NSLog(@"Keys: %@", [mutableDictionary allKeys]); // @[@"someKey"]
So to recap, here are the four values representing nothing that every Objective-C programmer should know about:
- nil Nil NULL NSNull
- NSNULL nil NULL Nil
- nil Nil NULL NSNull
- nil / Nil / NULL / NSNull
- nil / Nil / NULL / NSNull
- nil / Nil / NULL / NSNull
- nil Nil NULL NSNull
- nil/Nil/NULL/NSNull
- nil/Nil/NULL/NSNull
- nil/Nil/NULL/NSNull
- nil/Nil/NULL/NSNull
- nil / Nil / NULL / NSNull
- nil、Nil、NULL、NSNull
- nil / Nil / NULL / NSNull
- nil / Nil / NULL / NSNull
- nil、Nil、NULL、NSNull
- nil / Nil / NULL / NSNull
- nil / Nil / NULL / NSNull
- upcoj2129
- C/C++实现文件的备份
- 进化博弈理论
- SQL Server 2008高可用性的优缺点描述
- 验证后台线程的java多线程技术
- nil / Nil / NULL / NSNull
- 使用JID来进行Java对象的高性能序列化
- 偷Microsoft师学MFC艺:且看C++如何支持反射
- Android之rild进程启动源码分析
- Java解析XML的四种方式详解与比较
- 常用 Java 静态代码分析工具的分析与比较
- 长江电力资产注入预期强烈 方案尚在研究中
- ListView的Item被点击和其中的Button被点击同时生效
- javascript 实现淘宝客单页面的优化