cocoa arithmetic

Discussion in 'Mac Programming' started by medasmx, Dec 4, 2008.

  1. medasmx macrumors member

    Joined:
    Nov 9, 2008
    #1
    Below is the code for a simple program that inputs an integer in one textbox, then outputs it in another when a button is pushed. It works OK.

    #import <Cocoa/Cocoa.h>


    @interface fourthtryasm : NSObject {
    IBOutlet NSTextField*input;
    IBOutlet NSTextField*output;
    }
    -(IBAction)asmbutton:(NSButton*)sender;

    @end

    #import "fourthtryasm.h"


    @implementation fourthtryasm

    -(IBAction)asmbutton:(NSButton*)sender;
    {
    NSInteger integer=[input intValue];
    [output setIntValue:integer];
    }

    @end

    What I really wanted to do was to have 5 added to the inputted number when it was put in the second text box. I tried various things. The first attempt was the following --

    [output setIntValue:[integer+5]];

    The second was something like this--

    [output setIntValue:[[integer+5] intValue]]

    None of these worked, of course. Can someone tell me how to get this to work, and then let me know what ?class methods I should use to do arithmetic. Thanks.

    Adam
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    NSInteger is not a Class. It is just typedef'd to an integer thing. So treat things declared NSInteger like primitives:
    Code:
    [output setIntValue:(integer + 5)];
    
    Also, please don't name variables things like integer. In some languages an integer is defined using the keyword integer. If you want something that is generic, just call it myInt or myInteger.

    -Lee
     
  3. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #3
    For correctness' sake, with NSInteger, don't use setIntValue:, use setIntegerValue:, or else in 64-bit, your numbers might be unexpectedly truncated.
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Good times, didn't check the API reference.

    -Lee
     

Share This Page