0

I am 100% brand new to iOS. I am watching Stanford U's videos online to learn iOS. I spent hours slowly going through the second video meticulously ensuring I didn't goof on a line of code. Everything was perfect until - Wouldn't ya know it - the LAST minute of coding.

My wife and I spent a good amount of time trying to figure it out but my code matches the professor's as best we can tell. Please help me. I am loving learning through these videos but I can't move on until I have this functioning and take time to review it.

#import "CalculatorViewController.h"
#import "CalculatorBrain.h"

@interface CalculatorViewController()
@property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
@property (nonatomic, strong) CalculatorBrain *brain;
@end 

@implementation CalculatorViewController

@synthesize display  = _display;
@synthesize userIsInTheMiddleOfEnteringANumber = 
_userIsInTheMiddleOfEnteringANumber;
@synthesize brain = _brain;

- (CalculatorBrain *)brain
{
    if (!_brain) _brain = [[CalculatorBrain alloc] init];
    return _brain;
}

- (IBAction)digitPressed:(UIButton *)sender 
{
    NSString *digit = sender.currentTitle;
        if (self.userIsInTheMiddleOfEnteringANumber) 
        {
             self.display.text = [self.display.text stringByAppendingString:digit];
        }
    else 
    {
        self.display.text = digit;
        self.userIsInTheMiddleOfEnteringANumber = YES;
    }
}
- (IBAction)enterPressed 
{
    [self.brain pushOperand:[self.display.text doubleValue]];
    self.userIsInTheMiddleOfEnteringANumber = NO;
}

- (IBAction)operationPressed:(id)sender 
{
    if (self.userIsInTheMiddleOfEnteringANumber) [self enterPressed];
    double result = [self.brain performOperation:sender.currentTitle];  // <- HERE IS THE       LINE IN QUESTION

    NSString *resultString = [NSString stringWithFormat:@"%g", result];
    self.display.text = resultString;
}

@end

CalculatorBrain.m


#import "CalculatorBrain.h"

@interface CalculatorBrain()
@property (nonatomic, strong) NSMutableArray *operandStack;
@end

@implementation CalculatorBrain

@synthesize operandStack = _operandStack;

- (NSMutableArray *) operandStack
{
    if (_operandStack == nil) _operandStack = [[NSMutableArray alloc] init];
    return _operandStack;
}

- (void)pushOperand:(double)operand
{
    [self.operandStack addObject:[NSNumber numberWithDouble:operand]];
}

- (double)popOperand
{
    NSNumber *operandObject = [self.operandStack lastObject];
    if (operandObject) [self.operandStack removeLastObject];
    return [operandObject doubleValue];
}

- (double)performOperation:(NSString *)operation
{
    double result = 0;

    if ([operation isEqualToString:@"+"])
    {
        result = [self popOperand] + [self popOperand];
    }
    else if ([@"*" isEqualToString:operation])
    {
        result = [self popOperand] * [self popOperand];
    }

    [self pushOperand:result];

    return result;
}

@end
Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
Shades
  • 285
  • 2
  • 3
  • 15
  • Could you also include your code for `performOperation:` in the CalculatorBrain class? – Craig Siemens Jul 16 '12 at 00:14
  • Please explain what the actual problem is. Are you getting a compiler error, a runtime crash, unexpected results? – jrturton Jul 16 '12 at 05:54
  • The problem is at the line in his CalculatorViewController.m (first pile of code) Scroll down to operationPressed and you will see that he marked it with // HERE IS THE LINE IN QUESTION. Didn't notice that till I reviewed his code one by one. Main problem should be with his `if()` statement not done properly as he missed out the `{}` – Yang Jie Domodomo Jul 16 '12 at 07:36
  • Thank you. I will look into that immediately. – Shades Jul 16 '12 at 14:41

1 Answers1

1

I think the problem is with your if statement not closing properly.

- (IBAction)operationPressed:(id)sender 
{
    if (self.userIsInTheMiddleOfEnteringANumber){ [self enterPressed];
    double result = [self.brain performOperation:sender.currentTitle];  

    NSString *resultString = [NSString stringWithFormat:@"%g", result];
    self.display.text = resultString;
}
}

or

- (IBAction)operationPressed:(id)sender 
    {
        if (self.userIsInTheMiddleOfEnteringANumber)
{
 [self enterPressed];
}
        double result = [self.brain performOperation:sender.currentTitle]; 

        NSString *resultString = [NSString stringWithFormat:@"%g", result];
        self.display.text = resultString;

    }

Suggest looking at the pdf file of the tutorial for a more concrete code source. Hope that help.

Yang Jie Domodomo
  • 685
  • 1
  • 8
  • 25