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

HyperSnake

macrumors member
Original poster
Jan 5, 2009
74
0
Switzerland
Hi I'm writing my very first program with a GUI right now, a simple calculator app. I have fixed all issues the compiler came with except that after the #import I get an error message like this:

error: syntax error before '=' token
error: syntax error before '=' token
error: syntax error before '=' token
error: syntax error before '=' token

Here is my @interface:

Code:
#import <Cocoa/Cocoa.h>

@interface BECalculator : NSObject {
    IBOutlet id textField;
	NSString *theOutput;
	double firstNumber = 0;
	double secondNumber = 0;
	double theResult;
}
- (IBAction)add:(id)sender;
- (IBAction)divide:(id)sender;
- (IBAction)equals:(id)sender;
- (IBAction)multiply:(id)sender;
- (IBAction)setFirstNumberTo1:(id)sender;
- (IBAction)setFirstNumberTo2:(id)sender;
- (IBAction)setFirstNumberTo3:(id)sender;
- (IBAction)setFirstNumberTo4:(id)sender;
- (IBAction)setFirstNumberTo5:(id)sender;
- (IBAction)setFirstNumberTo6:(id)sender;
- (IBAction)setFirstNumberTo7:(id)sender;
- (IBAction)setFirstNumberTo8:(id)sender;
- (IBAction)setFirstNumberTo9:(id)sender;
- (IBAction)subtract:(id)sender;
@end

Here is the @implementation:
Code:
#import "BECalculator.h"

[COLOR="SeaGreen"]//It is here I get the error message.[/COLOR]

@implementation BECalculator

- (IBAction)add:(id)sender 
{
	theOutput = [NSString stringWithString: @"+"];
}

- (IBAction)divide:(id)sender 
{
	theOutput = [NSString stringWithString: @"/"];
}

- (IBAction)equals:(id)sender 
{
    if (theOutput == @"+")
		theResult = firstNumber + secondNumber;
	else if (theOutput == @"-")
		theResult = firstNumber - secondNumber;
	else if (theOutput == @"*")
		theResult = firstNumber * secondNumber;
	else if (theOutput == @"/")
		theResult = firstNumber / secondNumber;
	
	[textField setDoubleValue: theResult];
}

- (IBAction)multiply:(id)sender 
{
     theOutput = [NSString stringWithString: @"*"];
}

- (IBAction)setFirstNumberTo1:(id)sender 
{
    if (firstNumber =! 0)
		secondNumber = 1;
	else
		firstNumber = 1;
}

- (IBAction)setFirstNumberTo2:(id)sender 
{
	if (firstNumber =! 0)
		secondNumber = 2;
	else
		firstNumber = 2;
}

- (IBAction)setFirstNumberTo3:(id)sender 
{
	if (firstNumber =! 0)
		secondNumber = 3;
	else
		firstNumber = 3;
}

- (IBAction)setFirstNumberTo4:(id)sender 
{
	if (firstNumber =! 0)
		secondNumber = 4;
	else
		firstNumber = 4;
}

- (IBAction)setFirstNumberTo5:(id)sender 
{
	if (firstNumber =! 0)
		secondNumber = 5;
	else
		firstNumber = 5;
}

- (IBAction)setFirstNumberTo6:(id)sender 
{
	if (firstNumber =! 0)
		secondNumber = 6;
	else
		firstNumber = 6;
}

- (IBAction)setFirstNumberTo7:(id)sender 
{
	if (firstNumber =! 0)
		secondNumber = 7;
	else
		firstNumber = 7;
}

- (IBAction)setFirstNumberTo8:(id)sender 
{
	if (firstNumber =! 0)
		secondNumber = 8;
	else
		firstNumber = 8;
}

- (IBAction)setFirstNumberTo9:(id)sender 
{
	if (firstNumber =! 0)
		secondNumber = 9;
	else
		firstNumber = 9;
}

- (IBAction)subtract:(id)sender 
{
	theOutput = [NSString stringWithString: @"-"];
}
@end

Thank you in advance,

Ben.
 

szymczyk

macrumors regular
Mar 5, 2006
187
17
Code:
@interface BECalculator : NSObject {
    IBOutlet id textField;
	NSString *theOutput;
	double firstNumber = 0;
	double secondNumber = 0;
	double theResult;
}

You're giving firstNumber and secondNumber initial values in the interface, which is illegal in Objective C. The following fix should eliminate the error:

Code:
double firstNumber;
double secondNumber;

Set firstNumber and secondNumber to 0 in an initialization method.
 

HyperSnake

macrumors member
Original poster
Jan 5, 2009
74
0
Switzerland
You're giving firstNumber and secondNumber initial values in the interface, which is illegal in Objective C. The following fix should eliminate the error:

Set firstNumber and secondNumber to 0 in an initialization method.

Thank you, the error messages are indeed gone. How do I put an initialization method into a GUI program. I don't see how I do it when not using main() and without affecting the if statements.
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
Also you can't compare strings the way you're doing it as when you do an '=' comparison on an object you're comparing the object addresses not the object contents.
 

szymczyk

macrumors regular
Mar 5, 2006
187
17
How do I put an initialization method into a GUI program.

NSObject has an init method that you override in your BECalculator class. Here's an example of what you would write.

Code:
- (id)init 
{
    self = [super init];
    if (self != nil) {
        // initialization code
        firstNumber = 0.0;
        secondNumber = 0.0;
    }
    return self;
}

Put that code in your implementation file, BECalculator.m.
 

HyperSnake

macrumors member
Original poster
Jan 5, 2009
74
0
Switzerland
NSObject has an init method that you override in your BECalculator class. Here's an example of what you would write.

Code:
- (id)init 
{
    self = [super init];
    if (self != nil) {
        // initialization code
        firstNumber = 0.0;
        secondNumber = 0.0;
    }
    return self;
}

Put that code in your implementation file, BECalculator.m.

Thank you I will try it out

@caveman_uk I changed it to the isEqualToString method
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.