我正在使用 Xcode 4 编写一个简单的程序来根据用户输入生成文本。我成功读取了两个文本字段,但无法将结果发送到单独的 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 对象有一个功能,我认为我可能会在这个功能上搞砸了。我可以迭代地将格式化文本附加到哪种字符串?我叫什么方法?