Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Implementing the Model

Now it’s time to define the behavior of the functions you declared in Converter.h.

  1. In the Classes group of the Groups & Files menu, double-click Converter.m to open the file for editing.

  2. 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.

Define the convertCurrency Method

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.



< Previous PageNext Page > Hide TOC


Last updated: 2007-10-31




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice