DanMoore
DanMoore

Reputation: 661

Trouble with NSString: using a controller to set an NSTextField?

I am using Xcode 4 writing a simple program to generate text based on user input. Im reading from two text fields successfully, but I am unable to send the result to a separate NSTextField. I'm confident I've made all the right connections in IB, and I'm sending the NSTextField the setStringValue method. I have created a ScaleGenerator object that I believe may be improperly creating the string itself. My Controller header looks like this:

#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

The Controller implementation looks like this:

#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

My ScaleGenerator code looks like this:

#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

My ScaleGenerator object has one function, which is where I believe I may be messing things up. What kind of string can I iteratively append formatted text to?? And what method do I call??

Upvotes: 0

Views: 115

Answers (1)

ilmiacs
ilmiacs

Reputation: 2576

You have not allocated/initialized your NSMutableString. Therefore the message appendFormat: goes to nil. Replace the line

NSMutableString * result;

with

NSMutableString * result = [[NSMutableString alloc] init];

Upvotes: 1

Related Questions