01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

90 CHAPTER 4 Storing data in collections<br />

4.3.2 The NSValue class<br />

NSNumber is a special subclass of NSValue. While NSNumber provides a convenient and<br />

clean interface for boxing and unboxing numeric-based primitive types, NSValue<br />

allows you to box and unbox any C-style value at the expense of having a slightly more<br />

complex interface to program against.<br />

To box the value of an arbitrary C structure or union, you can take advantage of<br />

NSValue’s valueWithBytes:objCType: message. For example, the following code sample<br />

boxes a RentalProperty structure similar to those created in chapter 3:<br />

RentalProperty myRentalProperty =<br />

{270.0f, @"13 Adamson Crescent", TownHouse};<br />

NSValue *value = [NSValue valueWithBytes:&myRentalProperty<br />

objCType:@encode(RentalProperty)];<br />

This code snippet creates a copy of the rental property structure and places it inside<br />

the NSValue object created by the call to valueWithBytes:objCType:. The valueWith-<br />

Bytes argument is the address of the value you want to store in the NSValue instance,<br />

while objCType and the strange @encode statement allow you to tell NSValue what kind<br />

of data you want it to store.<br />

The process of unboxing an NSValue is different but still relatively straightforward.<br />

Rather than returning the unboxed value directly, the getValue message expects you<br />

to pass in a pointer to a variable it should populate with the value it contains, as demonstrated<br />

here:<br />

RentalProperty myRentalProperty;<br />

[value getValue:&myRentalProperty];<br />

On return, getValue: will have populated the myRentalProperty variable with the<br />

value you previously stored. You might be asking yourself, if NSNumber and NSValue<br />

allow you to get around the restriction of only being able to store objects in an array<br />

or dictionary, will a similar technique allow you to store the equivalent of a nil value?<br />

The answer is, yes.<br />

4.3.3 nil vs. NULL vs. NSNull<br />

In this chapter you learned that nil can’t be stored in an array or dictionary and it’s<br />

used to indicate the absence of a value or entry. But what exactly is nil?<br />

With a standard C-style pointer, the special value NULL indicates the absence of a<br />

value. nil represents the very same concept, except applied to a variable that’s<br />

designed to point to an <strong>Objective</strong>-C object.<br />

Because nil represents the absence of a value, it can’t be stored in an array or dictionary.<br />

But if you really want to store an empty or nil value, you can use a special<br />

class called NSNull. You can conceptualize NSNull as being another kind of boxing,<br />

similar to NSNumber or NSValue but specialized for storing nil.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!