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.

52 CHAPTER 2 Data types, variables, and constants<br />

Let’s put some of the concepts you’ve learned in this chapter into practice by completing<br />

the remaining tasks required to get the Rental Manager application to display<br />

details about the set of rental properties in your portfolio.<br />

2.5 Completing Rental Manager v1.0, App Store here we come!<br />

Now that you have a well-rounded understanding of how data can be stored in an<br />

<strong>Objective</strong>-C application and of the different data types involved, you’re ready to get<br />

back to the Rental Manager application.<br />

You may remember that when you left it earlier in the chapter, it was displaying a<br />

list of 25 rental properties, but each property was labeled “Rental Property x.” This<br />

didn’t provide you with much detail about each property! You now have the knowledge<br />

and skills required to resolve this problem.<br />

The first step is to define the information you would like to associate with each<br />

rental property. Some good details to start with could be<br />

■<br />

■<br />

■<br />

The physical address of the property<br />

The cost to rent the property per week<br />

The type of property (townhouse, unit, or mansion)<br />

To store this information, you can define a custom data type based on a structure.<br />

Open up the RootViewController.h header file for editing and insert the definitions<br />

found in the following listing at the bottom of the file’s existing content.<br />

Listing 2.3<br />

RootViewController.h<br />

typedef enum PropertyType {<br />

Unit,<br />

TownHouse,<br />

Mansion<br />

} PropertyType;<br />

typedef struct {<br />

NSString *address;<br />

PropertyType type;<br />

double weeklyRentalPrice;<br />

} RentalProperty;<br />

The first addition is the definition of an enumeration called PropertyType. It’s used<br />

to group the rental properties you manage into three distinct categories: units in a<br />

larger property, townhouses, or mansions.<br />

The second addition is a custom data type called RentalProperty that nicely<br />

encapsulates all of the details you want to store about a rental property. This typedef<br />

statement declares that the RentalProperty data type is a structure containing individual<br />

address, property type, and weekly rental price fields. If you pay close attention,<br />

you’ll notice there’s no name specified after the struct keyword. When using a<br />

typedef, it isn’t strictly necessary to name the struct because you usually don’t<br />

intend people to refer to the data type in this manner but via the name assigned to it<br />

with the typedef.

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

Saved successfully!

Ooh no, something went wrong!