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

< Previous PageNext Page > Hide TOC

Declare the Model Interface

The model for the Currency Converter is a simple class that encapsulates two variables: the amount in US dollars and the exchange rate. The model’s job is to multiply these two numbers and return the result. This means the model needs:

In this section:

Declare Member Variables
Declared Properties and Accessor Methods
Declare the Model Method: convertCurrency


Declare Member Variables

The two variables are defined between the braces of the converter class. Define the variables:

  1. If Converter.h is not already open for editing, double-click Converter.h in the Classes group of the Groups & Files menu. This opens the file in an editor window.

  2. Insert the highlighted line in Listing 3-1 into Converter.h

Listing 3-1  Declaration of the member variables in Converter.h

#import <Cocoa/Cocoa.h>
@interface Converter : NSObject {
    float sourceCurrencyAmount, rate;
}

Declared Properties and Accessor Methods

Objects encapsulate data, as explained in “Classes and Objects.” The scope of variables in an object is limited to that particular object—that is, the instance of that class. One instance of a customer class, for example, can only see its own data, not the data of any customer object. But say a specific customer needs to compare itself with another customer. In order for this to be possible, classes supply accessor methods to read and write to the data an object encapsulates. This gives the classes the discretion of what data they wish to share. For example, if a specific class does not want to share certain variables it encapsulates with another class, and only wants to use them for internal use, it can simply not have accessor methods for that variable. This is also true if a class wants external entities to have read-only access to its variables. In that case, a class will have methods that get values, but no methods that set values.

Objective-C 2.0 provides a feature called declared properties. A property declaration is, effectively, a shorthand for declaring the setter and getter for an attribute of an instance of a class. In addition to the declaration itself, there are directives to instruct the compiler to synthesize the accessors and to inform the compiler that you will provide the methods yourself at runtime. There are two parts to a property, its declaration and its implementation.

You declare a property as follows:

@property(attributes) Type variableNameList;

where attributes is a comma-separated list of keywords such as readwrite and copy, for example:

@property(copy) NSString *name;

For the purposes of Currency Converter, add the following line after the closing brace of the converter class in Converter.h:

@property(readwrite) float sourceCurrencyAmount, rate;

At compile time, the compiler treats this as if you had declared the following methods:

- (float)sourceCurrencyAmount;
- (void)setSourceCurrencyAmount:(float)newSourceCurrencyAmount;
- (float)rate;
- (void)setRate:(float)newRate;

Note: Objective-C 2.0 features are not compatible with a system running any operating system prior to Mac OS X 10.5. If you are on Mac OS X 10.4 or below, you must declare these methods manually.

There is currently no implementation for these methods. In the next section, you will implement the methods.

Declare the Model Method: convertCurrency

The one method the model requires is a simple function to multiply two the two values encapsulated by the converter class.

Add the following line as the next line of code in Converter.h:

- (float)convertCurrency;

Although this method takes no arguments, it multiplies two values that are not passed to the method, nor are they instantiated in the method. This is possible because the method is part of the converter class, and it has access to the converter class’s member variables. It gets to treat those variables as if they were already defined as local variables. It is common practice to use accessor methods even when the variable is in scope, but for this application you will access them using the command self.variableName (where self is a keyword that is a pointer to the current instance of the object). In the next section, you will define the behavior of the converter and get to see how this works.



< 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