Mac OS X Reference Library Apple Developer
Search

Defining the Model

Model objects contain special knowledge and expertise. They hold data and define the logic that manipulates that data. For example, a customer object, common in business applications, is a model object. In Currency Converter, the model class you’re going to create is the converter class. In the MVC design pattern, instances of a model class do not communicate directly with the user interface.

In this chapter, you will create the model for your application. First, you will learn how to create a new class in Xcode. Then, you will define the interface for your model before implementing it. Finally, you will implement the entire functionality of the class. In the process, you will learn some of the syntax of Objective-C, including how to declare variables and methods. You will also be introduced to the concept of declared properties—a feature in Objective-C 2.0 that makes writing accessor methods incredibly quick and simple.

Specify the Model Class

  1. Select the Classes group in the Groups & Files list.

  2. Choose File > New File.

  3. Select Objective-C class in the Cocoa Class group. Leave it as a subclass of NSObject, and click Next.

    image: ../Art/new_class.jpg
  4. Name your new class Converter and click Finish.

Xcode automatically puts the interface (Converter.h) and implementation (Converter.m) files for your new class in the Classes group and opens the interface file so you can get right to work.

Declare the Model Interface

The model for the currency converter is a simple class that contains two variables: a dollar 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:

Declare Instance 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 third line in Listing 3-1 into Converter.h in the location shown.

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 other customer object. But say a specific customer needs to compare itself with another customer. In order for this to be possible, classes supply accessor method to read and write to the data an object encapsulates. This gives the classes discretion over what data they share. For example, if a class only wants to use certain variables for internal use, it can simply not have accessor methods for those variables. Classes can also provide external entities with read-only access to its data. In that case, the class provides methods that get values for that data, 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;

Your converter class wants to provide access to its data to external objects, so you need to declare accessor methods.

  1. Place your insertion point after the closing brace of the instance method declarations in Converter.h, just before the @end directive.

  2. Add the following property declaration for your two instance variables.

    @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;

The property declaration simply declares what the accessor methods will look like. In the next section, you will provide an implementation for the accessor methods.

Declare the Model Method: convertCurrency

The one method the model requires is a simple function to multiply 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 nonetheless provides the result of multiplying the source currency amount with the conversion rate. This is possible because instance methods have access to the instance variables of their object. At runtime, the instance method treats all instance variables as if they were local variables. It is common practice to use accessor methods even when the variable is in scope. This practice makes it clear that the code is accessing an instance variable, rather than a local variable.

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. Similarly, you can 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.

For more information on properties and the various options available, see Declared Properties.

Define the convertCurrency Method

Insert the definition of the convertCurrency method into Converter.m, as shown in Listing 3-2.

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 instance variables and returns the result. Notice the use of the notation self.sourceCurrencyAmount to access the sourceCurrencyAmount instance variable. self is an Objective-C keyword that stands for “the object executing the method”. Declared properties can be accessed using the familiar dot notation, just like accessing fields in a struct.

What’s Next?

You just defined and implemented the basic functionality of your application by creating the model. In the next chapter, you will create the view—the user interface for the application.




Last updated: 2009-08-03

Did this document help you? Yes It's good, but... Not helpful...