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.

120 CHAPTER 5 Creating classes<br />

One thing to note is that, although declared properties can automatically provide<br />

getter and setter implementations, they won’t generate code in dealloc to free their<br />

associated memory. If your class contains properties using retain or copy semantics,<br />

you must manually place code in your class’s dealloc method to clean up their memory<br />

usage. In listing 5.8 this is demonstrated by the address property. When destroying<br />

a rental property object, you want the copy of the address string you made in the<br />

property’s setter to be cleaned up.<br />

Finally, most dealloc method implementations end with a call to [super dealloc].<br />

This gives the superclass a chance to free any resources it has allocated. Notice<br />

that the order here is important. Unlike with an init implementation, where it’s<br />

important to give the superclass a chance to initialize before you initialize your own<br />

content, with a dealloc method, you must tidy up your own resources, and then give<br />

the superclass a chance to tidy up.<br />

5.6 Using the class in the Rental Manager application<br />

Now that you’ve created a class called CTRentalProperty and gone through various<br />

iterations to improve its source code, let’s update the Rental Manager application.<br />

The first step is to open RootViewController.h and remove the existing definitions<br />

for the PropertyType enumeration and RentalProperty structure. Replace them with<br />

the CTRentalProperty class. At the same time, you can add an NSArray-based instance<br />

variable to store the list of rental properties. With these changes made, you should<br />

end up with RootViewController.h looking similar to the following listing.<br />

Listing 5.9<br />

RootViewController.h storing rental properties in an object-oriented manner<br />

@interface RootViewController : UITableViewController {<br />

NSDictionary *cityMappings;<br />

NSArray *properties;<br />

}<br />

@end<br />

Using an NSArray to store the rental properties enables you to eventually add and<br />

remove properties while your application runs. This is something you can’t easily do<br />

with the C-style array used previously.<br />

In comparison, RootViewController.m requires a number of larger source code<br />

changes, so replace its existing contents with the following listing.<br />

Listing 5.10<br />

RootViewController.m updated to use the CTRentalProperty class<br />

#import "RootViewController.h"<br />

#import "CTRentalProperty.h"<br />

@implementation RootViewController<br />

- (void)viewDidLoad {<br />

[super viewDidLoad];<br />

b<br />

Import the CTRentalProperty<br />

definition<br />

NSString *path = [[NSBundle mainBundle]<br />

pathForResource:@"CityMappings"

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

Saved successfully!

Ooh no, something went wrong!