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

< 前ページ次ページ > 目次を隠す

モデルの実装

ここでは、Converter.hで宣言した関数の動作を定義します。

  1. 「グループとファイル」メニューのClassesグループで、Converter.mをダブルクリックして編集用にファイルを開きます。

  2. 2つのメンバ変数(sourceCurrencyAmountrate)に対してgetterとsetterを作成します。

    @propertyを使って、Converter.hでgetterおよびsetterメソッドのプロトタイプを作成したことを思い出してください。あるプロパティに対してgetterメソッドとsetterメソッド(あるいは読み取り専用プロパティの場合はgetterメソッドのみ)の実装を指定する場合は、これ以上必要な作業はありません。しかし一般には、@implementationブロックで、コンパイラにアクセサメソッドの生成を指示する@synthesizeディレクティブを使います。

    下の行を、Converter.m@implementation Converter行の後に追加します。

    @synthesize sourceCurrencyAmount, rate;

    この行は、Converter.hファイルで設定したプロパティに基づいて、変数sourceCurrencyAmountおよびrateに対するgetterメソッドとsetterメソッドの本体を定義します。

    注: 使用可能なプロパティと各種オプションの詳細については、『The Objective-C 2.0 Programming Language』の「Properties」のセクションを参照してください。

convertCurrencyメソッドを定義する

リスト 3-2のハイライトされている行を、Converter.mに挿入します。

リスト 3-2  Converter.mでのconvertCurrencyメソッドの定義

#import "Converter.h"
 
@implementation Converter
@synthesize sourceCurrencyAmount, rate;
 
- (float)convertCurrency {
    return self.sourceCurrencyAmount * self.rate;
}
 
@end

convertCurrencyメソッドは、converterクラスの2つのメンバ変数の値を乗じ、その結果を返します。



< 前ページ次ページ > 目次を隠す


Last updated: 2008-01-24




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