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.

Declared properties<br />

113<br />

Although @synthesize is commonly used in tandem with the @property directive,<br />

its use is entirely optional. The @synthesize directive is intelligent and will generate<br />

getter or setter methods only if it can’t find a suitable method already declared elsewhere<br />

in the @implementation section. This can be helpful if you’d like the compiler<br />

to generate most of your getter and setter methods, but you’d like to override one or<br />

two of them in order to perform special behavior, such as logging or input validation.<br />

An example of this functionality is demonstrated here:<br />

@synthesize rentalPrice;<br />

- (void)setRentalPrice:(float)newRentalPrice {<br />

NSLog(@"You changed the rental per week to %f", newRentalPrice);<br />

rentalPrice = newRentalPrice;<br />

}<br />

In this case, the @synthesize directive generates a getter method for the rentalPrice<br />

property but uses the explicitly specified setRentalPrice: setter.<br />

By default, the @synthesize directive assumes that a property named foo will store<br />

its value in an instance variable also named foo. If for some reason this isn’t desirable,<br />

you can override the name of the instance variable in the @synthesize statement. The<br />

following declaration, for example, synthesizes a rentalPrice property by using an<br />

instance variable called rentalPerWeek:<br />

@synthesize rentalPrice = rentalPerWeek;<br />

Use of the @synthesize directive results in the compiler generating, or synthesizing,<br />

the methods required for a property. Unlike manually written code, the generated<br />

code is well tested, thread safe, and efficient while handling the nuances of memory<br />

management without a second thought from the developer. Using properties not only<br />

reduces the amount of code you need to write but also ensures you avoid common<br />

opportunities to introduce bugs. You can have your cake and eat it too!<br />

5.4.3 Dot syntax<br />

With your current knowledge of <strong>Objective</strong>-C syntax to obtain the current address of a<br />

rental property, you must send the object an address message, as demonstrated here:<br />

CTRentalProperty *property = ...some rental property...<br />

NSString *address = [property address];<br />

Likewise, you could update the address property by sending the setAddress: message:<br />

CTRentalProperty *property = ...some rental property...<br />

[property setAddress:@"45 Some Lane"];<br />

<strong>Objective</strong>-C, however, provides an alternative syntax for use with properties. This syntax<br />

is based on the dot (.) operator and may be more comfortable for developers with<br />

a C or Java background:<br />

CTRentalProperty *property = ...some rental property...<br />

NSString *address = property.address;<br />

NSLog(@"Old address is: %@", address);

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

Saved successfully!

Ooh no, something went wrong!