nil / Nil / NULL / NSNull
来源:互联网 发布:caxa机械制图软件下载 编辑:程序博客网 时间:2024/04/20 22:28
http://nshipster.com/nil/
nil / Nil / NULL / NSNull
Written by Mattt Thompson on Jan 7th, 2013
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
- iOS中播放gif动画
- instancetype
- HDU 1561 The more, The Better
- CSS 高级语法
- Aptana Studio 3 汉化简体中文版
- nil / Nil / NULL / NSNull
- (转)微小说--我没有白吃
- 中国和欧盟激烈争吵,法国率先对华服软令英意傻眼
- ubuntu学习(一)之MBR
- 查询执行最慢的query的DMV
- NSLocalizedString
- __attribute__
- Reader Submissions
- 第五章 例5.4