0

Xcode 4 を使用して、ユーザー入力に基づいてテキストを生成する簡単なプログラムを作成しています。2 つのテキスト フィールドからの読み取りは成功しましたが、結果を別の NSTextField に送信できません。私は IB ですべての適切な接続を行ったと確信しており、NSTextField に setStringValue メソッドを送信しています。文字列自体を不適切に作成している可能性があると思われる ScaleGenerator オブジェクトを作成しました。私のコントローラーのヘッダーは次のようになります。

#import <Cocoa/Cocoa.h>

@interface Controller : NSObject
{
    IBOutlet NSTextField * notesField;
    IBOutlet NSTextField * midiField;
    IBOutlet NSTextField * resultField;
    IBOutlet id scalegenerator;
}    
- (IBAction) generate: (id) sender;
@end // Controller

コントローラーの実装は次のようになります。

#import "Controller.h"
#import "ScaleGenerator.h"
#import <Foundation/foundation.h>

@implementation Controller

- (IBAction) generate: (id) sender
{
    int midinote;
    int notes;
    NSString * scale;

    midinote = [midiField intValue];
    if(midinote < 0 || midinote > 127) {
        NSRunAlertPanel(@"Bad MIDI", @"That MIDI note is out of range! (0 - 127)", @"OK", nil, nil);
        return;
    }

    notes = [notesField intValue];
    if(notes < 2 || notes > 24) {
        NSRunAlertPanel(@"Bad Scale Size", @"You must have at least two notes in your scale, and no more than 25 notes!", @"OK", nil, nil);
        return;
    }

    scale = [scalegenerator generateScale: midinote and: notes];

    [resultField setStringValue:scale];   
}  
@end

私の ScaleGenerator コードは次のようになります。

#import "ScaleGenerator.h"
#import <math.h>

@implementation ScaleGenerator

- (NSMutableString *) generateScale: (int) midinote and: (int) numberOfNotes
{
    double frequency, ratio;
    double c0, c5;
    double intervals[24];
    int i;
    NSMutableString * result;

    /* calculate standard semitone ratio in order to map the midinotes */
    ratio = pow(2.0, 1/12.0);       // Frequency Mulitplier for one half-step
    c5 = 220.0 * pow(ratio, 3);     // Middle C is three semitones above A220
    c0 = c5 * pow(0.5, 5);          // Lowest MIDI note is 5 octaves below middle C
    frequency = c0 * pow(ratio, midinote); // the first frequency is based off of my midinote

    /* calculate ratio from notes and fill the frequency array */
    ratio = pow(2.0, 1/numberOfNotes);
    for(i = 0; i < numberOfNotes; i++) {
        intervals[i] = frequency;
        frequency *= ratio;
    }

    for(i = 0; i < n; i++){
        [result appendFormat: @"#%d: %f", numberOfNotes + 1, intervals[i]];
    }

    return (result);
}
@end // ScaleGenerator

私の ScaleGenerator オブジェクトには関数が 1 つあります。書式設定されたテキストを繰り返し追加できるのは、どのような文字列ですか?? そして、どのメソッドを呼び出すのですか??

4

1 に答える 1

1

を割り当て/初期化していませんNSMutableString。したがって、メッセージappendFormat:はに送られnilます。行を置き換えます

NSMutableString * result;

NSMutableString * result = [[NSMutableString alloc] init];
于 2012-10-17T18:39:20.240 に答える