Now it’s time to define the behavior of the functions you declared in Converter.h.
In the Classes group of the Groups & Files menu, double-click Converter.m to open the file for editing.
Create the getters and setters for the two member variables—sourceCurrencyAmount and rate.
Remember you used @property to create the prototypes for the getter and setter methods in Converter.h. If you provide a getter and setter (or just a getter in the case of a read-only property) method implementation for a property, you don’t need to do anything more. Commonly, however, you use the @synthesize directive in @implementation blocks to trigger the compiler to generate accessor methods for you.
Add the following line into Converter.m after the @implementation Converter line:
@synthesize sourceCurrencyAmount, rate; |
This line defines the body of the getter and setter methods for the variables sourceCurrencyAmount and rate based on the properties you set in the Converter.h file.
Note: For more information on properties and the various options available, read “Properties” in The Objective-C 2.0 Programming Language.
Insert the highlighted lines in Listing 3-2 into Converter.m.
Listing 3-2 Definition of the convertCurrency method in Converter.m
#import "Converter.h" |
@implementation Converter |
@synthesize sourceCurrencyAmount, rate; |
- (float)convertCurrency { |
return self.sourceCurrencyAmount * self.rate; |
} |
@end |
The convertCurrency method multiplies the values of the converter class’s two member variables and returns the result.
Last updated: 2007-10-31