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.

Boxing<br />

89<br />

that doesn’t stop you from shipping it by temporarily placing it inside a box that meets<br />

FedEx’s requirements.<br />

Unfortunately, <strong>Objective</strong>-C doesn’t provide for the automatic boxing and unboxing<br />

of primitive data types. Therefore, to store an integer or other primitive value in<br />

an array or dictionary, you must perform the boxing and unboxing yourself. This is<br />

the purpose of the NSNumber class.<br />

4.3.1 The NSNumber class<br />

NSNumber is an <strong>Objective</strong>-C class that can be used to wrap a value of a primitive data<br />

type such as int, char, or BOOL into an object and then allow that value to be extracted<br />

at a later stage. It’s most useful for allowing you to place values of primitive data types<br />

into data structures such as NSArray or NSDictionary that can only store objects.<br />

Manually boxing a primitive value into an NSNumber instance is fairly straightforward:<br />

you call one of NSNumber’s factory methods, such as numberWithInt:<br />

NSNumber *myNumber = [NSNumber numberWithInt:5];<br />

There are similar factory messages called numberWithFloat:, numberWithBool:, and<br />

so on, to allow boxing of other common primitive data types.<br />

Now that the integer value is boxed inside an NSNumber (which is an object), you<br />

can store it in your array, as you originally intended:<br />

[myArray addObject:myNumber];<br />

Boxing the integer inside an NSNumber also means that when you go to fetch the value<br />

from the array, you need to perform the reverse operation to extract the primitive<br />

value from the NSNumber instance. You can do this with a code snippet similar to the<br />

following that uses NSNumber’s intValue message:<br />

NSNumber *myNumber = [myArray objectAtIndex:0];<br />

int i = [myNumber intValue];<br />

The NSNumber class has various other methods that conform to the naming convention<br />

xxxValue, where xxx is replaced with the name of a primitive data type. It’s not<br />

an error to box a value via numberWithInt: and then retrieve it via a method such as<br />

floatValue. Although you stored an integer and fetched a float, this is acceptable. In<br />

this scenario, the NSNumber class performs a typecast operation similar to those discussed<br />

in chapter 2 to convert the value into your desired data type.<br />

A little bit of fancy footwork was required, but in the end, boxing and unboxing<br />

primitive values so you can use them as if they were objects wasn’t too bad. What happens,<br />

though, if you want to store one or more RentalProperty structures in an<br />

NSArray? These structures are also not objects, but it’s doubtful that the NSNumber<br />

class has a numberWithRentalPropertyDetail method available for you to box them.<br />

The answer to this conundrum is another closely related class called NSValue.

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

Saved successfully!

Ooh no, something went wrong!