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.

Declared properties<br />

109<br />

- (PropertyType)propertyType {<br />

return propertyType;<br />

}<br />

@end<br />

Most of the methods in CTRentalProperty.h are relatively straightforward (for example,<br />

setRentalPrice: B updates the value of an instance variable). One method that<br />

deserves extra attention is setAddress: because it must deal with memory management<br />

issues. Rather than storing the new address in the instance variable directly, it<br />

makes a copy d (taking care to free any previous address first c). This is done so<br />

code similar to the following will work as expected:<br />

NSMutableString *anAddress =<br />

[NSMutableString stringWithString:@"13 Adamson Crescent"];<br />

myRental.address = anAddress;<br />

[anAddress replaceOccurrencesOfString:@"Crescent"<br />

withString:@"Street"<br />

options:NSCaseInsensitiveSearch<br />

range:NSMakeRange(0, [anAddress length])];<br />

NSLog(@"The address is %@", myRental.address);<br />

Most developers would expect this code snippet to indicate the address is 13 Adamson<br />

Crescent even though the string variable is updated to 13 Adamson Street. Making a<br />

copy of the string provided to setAddress: ensures this works as expected. Writing<br />

code to handle conditions like these can get arduous and error prone fairly quickly. It’s<br />

just the kind of thing you let slip toward the end of a late-night, caffeine-aided coding<br />

session. Luckily, <strong>Objective</strong>-C provides special features to make writing such code easier.<br />

5.4 Declared properties<br />

In listings 5.1 and 5.2, much of the code is related to the declaration and implementation<br />

of getter and setter methods to provide safe access to associated instance variables<br />

and provide extensibility points for future logging or validation. <strong>Objective</strong>-C can automatically<br />

implement most of the code required to implement these methods using a<br />

feature called Declared Properties.<br />

A property allows you to describe the intent of a setter and getter method pair<br />

while leaving it up to the compiler to provide the actual implementation. This is more<br />

convenient to write and ensures consistent code quality.<br />

5.4.1 @property syntax<br />

The first step in using a property is to declare its presence in the @interface section<br />

of a class. To do this, you use a special @property directive. As an example, you can<br />

declare a property called rentalPrice of type float as follows:<br />

@property float rentalPrice;<br />

You can consider this property declaration as being equivalent to manually declaring<br />

the following two methods:<br />

- (float)rentalPrice;<br />

- (void)setRentalPrice:(float)newRentalPrice;

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

Saved successfully!

Ooh no, something went wrong!