Sidd Menon
Sidd Menon

Reputation: 886

Objective-C: EXC_BAD_ACCESS and doesn't respond

Here's the header file:

@interface calc : NSObject {
    IBOutlet NSTextField *tf1;
    IBOutlet NSTextField *tf2;
    IBOutlet NSTextField *ansr;
    int s;
}

- (IBAction)add:(id)sender;

@end

And here's the implementation file:

@implementation calc

- (IBAction)add:(id)sender
{
    s = [tf1 intValue] + [tf2 intValue];
    [ansr setStringValue:(@"%@", s)];



}
- (void) dealloc {
    [tf1 release];
    tf1 = nil;
    [tf2 release];
    tf2 = nil;
    [ansr release];
    ansr = nil;
    [super dealloc];
}
@end

Upvotes: 0

Views: 131

Answers (2)

deanWombourne
deanWombourne

Reputation: 38475

s is an int, not an object.

Try this instead :

- (IBAction)add:(id)sender
{
    s = [tf1 intValue] + [tf2 intValue];
    [ansr setIntValue:s];

Upvotes: 3

gcamp
gcamp

Reputation: 14662

setStringValue: doesn't accept more than one argument like printf and stringWithFormat: does.

You should do something like

s = [tf1 intValue] + [tf2 intValue];
NSString* result = [NSString stringWithFormat:@"%i", s];
[ansr setStringValue:result];

Upvotes: 3

Related Questions