Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble with NSString: using a controller to set an NSTextField?
    primarykey
    data
    text
    <p>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:</p> <pre><code>#import &lt;Cocoa/Cocoa.h&gt; @interface Controller : NSObject { IBOutlet NSTextField * notesField; IBOutlet NSTextField * midiField; IBOutlet NSTextField * resultField; IBOutlet id scalegenerator; } - (IBAction) generate: (id) sender; @end // Controller </code></pre> <p>The Controller implementation looks like this:</p> <pre><code>#import "Controller.h" #import "ScaleGenerator.h" #import &lt;Foundation/foundation.h&gt; @implementation Controller - (IBAction) generate: (id) sender { int midinote; int notes; NSString * scale; midinote = [midiField intValue]; if(midinote &lt; 0 || midinote &gt; 127) { NSRunAlertPanel(@"Bad MIDI", @"That MIDI note is out of range! (0 - 127)", @"OK", nil, nil); return; } notes = [notesField intValue]; if(notes &lt; 2 || notes &gt; 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 </code></pre> <p>My ScaleGenerator code looks like this:</p> <pre><code>#import "ScaleGenerator.h" #import &lt;math.h&gt; @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 &lt; numberOfNotes; i++) { intervals[i] = frequency; frequency *= ratio; } for(i = 0; i &lt; n; i++){ [result appendFormat: @"#%d: %f", numberOfNotes + 1, intervals[i]]; } return (result); } @end // ScaleGenerator </code></pre> <p>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??</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload