nil / Nil / NULL / NSNull

來源:互聯網
上載者:User

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 NSObjects
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:

Symbol Value Meaning
NULL (void *)0 literal null value for C pointers
nil (id)0 literal null value for Objective-C objects
Nil (Class)0 literal null value for Objective-C classes
NSNull [NSNull null] singleton object used to represent null

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.