01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Making the Rental Manager application data driven<br />

91<br />

To store a nil value in an array or dictionary, you can use the null factory method<br />

located on the NSNull class:<br />

NSNull myValue = [NSNull null];<br />

[myArray addObject:myValue];<br />

An instance of the NSNull class is an object, so it can be stored in an NSArray or<br />

NSDictionary instance or used anywhere else an object is expected. Because an<br />

NSNull instance represents the absence of a value, the class doesn’t provide any way in<br />

which to extract a value, so when it comes time to pull your NSNull value out of an<br />

array, you can compare it against itself:<br />

id value = [myArray objectAtIndex:5];<br />

if (value == [NSNull null]) {<br />

NSLog(@"The 6th element within the array was empty");<br />

} else {<br />

NSLog("@The 6th element within the array had the value %@", value);<br />

}<br />

Unlike the problems you had comparing NSString instances with the == operator,<br />

this technique works for NSNull instances. The statement [NSNull null] doesn’t create<br />

a new object each time it’s called. Instead, [NSNull null] always returns the<br />

memory location of a sole instance of the NSNull class. This means that two variables<br />

that are storing a pointer to an NSNull value will have an identical address, and<br />

hence the == operator will consider them the same. This is an example of the Singleton<br />

design pattern.<br />

This completes our coverage of the basics of Foundation Kit’s core classes and the<br />

data structure implementations they provide. With your newfound knowledge, you<br />

can also resolve the issue with the Rental Manager application highlighted at the end<br />

of chapter 3.<br />

4.4 Making the Rental Manager application data driven<br />

One outstanding problem with the Rental Manager application is that the mapping<br />

of city names to geographical locations is hardcoded, when what you ideally want<br />

is for it to be data driven and easily updateable without the need to recompile the<br />

entire application.<br />

These concepts nicely match the functionality provided by an NSDictionary object<br />

(with the key being the city name and the value being its matching location type) that<br />

obtains its initial contents from a plist file.<br />

Let’s add a new plist file called CityMappings.plist to your project. As you may<br />

expect, the New File dialog (located in File > New > New File… and shown in figure 4.1)<br />

has a suitable template to get you started. Once the new file is added to your project,<br />

Xcode presents a graphical plist file editor that quickly allows you to edit its contents<br />

without worrying about balancing XML open and closing brackets, and so on. Using<br />

this editor, populate your new plist file with the contents shown in figure 4.2.

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

Saved successfully!

Ooh no, something went wrong!