Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

medasmx

macrumors member
Original poster
Nov 9, 2008
89
0
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
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.
Code:
#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 --

Code:
[output setIntValue:[integer+5]];
The second was something like this--
Code:
[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

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
 

kpua

macrumors 6502
Jul 25, 2006
294
0
For correctness' sake, with NSInteger, don't use setIntValue:, use setIntegerValue:, or else in 64-bit, your numbers might be unexpectedly truncated.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.