Basic Problem reading input from NSTextCell in Xcode

Discussion in 'Mac Programming' started by AlexRutherford, Oct 22, 2009.

  1. AlexRutherford macrumors newbie

    Oct 22, 2009
    Hi, I'm new to Objective C and just doing some basics.

    All I want to do is;

    - read a value from one text field
    - double it
    - output it to another text field

    But I'm stuck on the first, each time I try to read the value in it returns a value of 0 even though the entry is clearly 1 or 3 or 100.

    This is the snippet

    input=[inputfield floatValue];

    Where inputfield is the NSTextCell box and input is a float.

    The rest of the code is below in case it is useful.

    Please help, going insane!


    #import "readandwrite.h"

    @implementation readandwrite

    @synthesize input, output;

    -(IBAction)calculate: (id) sender

    // float input;

    input=[inputfield floatValue];
    printf("Input is %f\n",input);
    printf("Output is %f\n",output);
    [outputfield setFloatValue:eek:utput];



    #import <Cocoa/Cocoa.h>

    @interface readandwrite : NSObject {
    char input;
    float output;
    IBOutlet NSTextField *outputfield;
    IBOutlet NSTextField *inputfield;


    @property(readwrite) float output;
    @property(readwrite) char input;


  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    1. Make sure inputfield actually contains a non-nil object. Messaging nil is valid in Objective-C. The return value from messaging nil is 0 or nil.

    2. Your instance variable 'input' is actually declared as 'char' in readwrite.h. C (and thus Objective-C) will happily and silently convert from float to char, even though this throws away a huge amount of precision and dynamic range.

    3. This code:
    printf("Input is %f\n",input);
    will not work the way you seem to think it will, since the type of 'input' is char.

    4. When posting code, please enclose it in CODE markers. It's the little icon that looks like #, and if you hover the cursor over it, the tooltip says "Wrap [*CODE] tags around selected text"

Share This Page