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.

106 CHAPTER 5 Creating classes<br />

To demonstrate that classes can contain methods other than simple getters and<br />

setters, the CTRentalProperty class also declares d two methods with the names<br />

increaseRentalByPercent:withMaximum: and decreaseRentalByPercent:with-<br />

Minimum:. These methods act as special setters that update the rentalPrice instance<br />

variable by performing a simple calculation based on the provided parameters.<br />

This class interface declaration is enough for anybody to go off and develop code<br />

that makes use of the CTRentalProperty class. It has outlined to the compiler, and to<br />

any interested users of the class, what kind of data you store and what kind of behavior<br />

they can expect from you. With the classes interface specified, it’s time to provide the<br />

compiler with the logic that implements the class.<br />

5.3 Providing an implementation for a class<br />

The @interface section of a class only declares what an object will look like. It doesn’t<br />

provide details on how methods are implemented. Instead, this source code is typically<br />

contained in a separate implementation (or method) file that commonly has the<br />

file extension *.m.<br />

Similar to the @interface section, the implementation of a class is started by an<br />

@implementation directive followed by the class name and ending with an @end directive:<br />

@implementation CTRentalProperty<br />

... method implementations ...<br />

@end<br />

The class name is required after the @implementation directive because it’s possible<br />

for a single *.m file to contain the implementations of multiple classes. With the location<br />

of a particular class’s implementation determined, let’s look at how you specify<br />

the behavior of a particular method.<br />

5.3.1 Defining method implementations<br />

You define the logic and behavior of a method by repeating its declaration. But<br />

instead of ending the declaration with a semicolon, you use a set of curly braces and<br />

provide the required logic that should be executed whenever the method is invoked.<br />

The following is one possible implementation of the setRentalPrice: method:<br />

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

NSLog(@"TODO: Change the rental price to $%f", newRentalPrice);<br />

}<br />

This implementation, however, simply logs a to-do message requesting you provide a<br />

more practical implementation. To flesh out the implementation, you must be able<br />

to access the instance variables associated with the current object. Luckily, this is<br />

easy to do.<br />

5.3.2 Accessing instance variables<br />

When the body of an instance method is declared, the method automatically has<br />

access to any instance variables associated with the current object. You can refer to

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

Saved successfully!

Ooh no, something went wrong!