im very new to objective c and making apps and not really sure why this is not building. here is my code that is failing:
CalculatorViewController.m
#import "CalculatorViewController.h"
@interface CalculatorViewController ()
@end
@implementation CalculatorViewController
-(CalculatorBrain *)brain
{
if (!brain){
brain = [[CalculatorBrain alloc] init];
}
return brain;
}
-(IBAction)digitPressed:(UIButton *)sender;
{
NSString *digit = [[sender titleLabel] text];
if (userIsInTheMiddleOfTypingANumber){
[display setText:[[display text] stringByAppendingString:digit]];
} else{
[display setText:digit];
userIsInTheMiddleOfTypingANumber = YES;
}
}
-(IBAction)operationPressed:(UIButton *)sender;
{
if (userIsInTheMiddleOfTypingANumber){
[[self brain] setOperand:[[display text] doubleValue]];
userIsInTheMiddleOfTypingANumber = NO;
}
NSString *operation = [[sender titleLabel] text];
double result = [[self brain] performOperation:operation];
[display setText:[NSString stringWithFormat:@"%g", result]];
}
@end
and CalculatorViewController.h
#import <UIKit/UIKit.h>
#import "CalculatorBrain.h"
@interface CalculatorViewController : UIViewController{
IBOutlet UILabel *display;
CalculatorBrain *brain;
BOOL userIsInTheMiddleOfTypingANumber;
}
-(IBAction)digitPressed:(UIButton *)sender;
-(IBAction)operationPressed:(UIButton *)sender;
@end
double result = [[self brain] performOperation:operation]; at the very end of the CalculatorViewController.m is where I get the error Initializing ‘double’ with an expression of incompatible type ‘void’
What does this mean or where should I look to fix this?
performOperation:will probably be declared asAs you can see, the return of this is
void. This means it doesn’t return anything, as such, you’re trying to assign a void return to a double, which is invalid.The problem will either be in the header file, or the implementation file where it has been implemented. Look at the method signature to see if it contains void.
To fix, either don’t assign to a double variable, or (more preferable I assume) allow
performOperation:to return a double value.