Ok, so I will preface this by saying this is NOT my code and I am doing this exercise to help learn. In fact, this came from a book and I have read over it several times and it appears correct.
ViewController.m
when I run this in the iOS simulator and click the equals button I get the following exception:
'NSInvalidArgumentException', reason: '-[__NSCFString appendString:]: nil argument'
for the following line of code
Ok, so I thought "The myCalculator" reference must not be instantiated and is set to nil.
So, I instantiate it in "clickEquals" (I get a warning that says I'm masking my instance variable (though Xcode won't let me instantiate that one)) and it works. It clears up the exception.
BUT there is a bit of a scope problem.
So I need some help.

ViewController.m
Code:
//
// ViewController.m
// Calculator
//
// Created by on 5/1/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "ViewController.h"
#import "Calculator.h"
#import "Fraction.h"
@implementation ViewController {
//instance variables (though I think I would of set these up as object properties
char op;
int currentNumber;
BOOL firstOperand, isNumerator;
Calculator *myCalculator; //I'm not sure why this isn't instantiated as an //object?
NSMutableString *displayString;
}
@synthesize display;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
//overrided the initiall loading
firstOperand = YES;
isNumerator = YES;
displayString = [NSMutableString stringWithCapacity:40];
Calculator *myCalculator = [[Calculator alloc]init];
//I'm guessing the above is the reference for the myCalculator object in //"clickEQUALS"
}
-(void)processDigit:(int)digit
{
currentNumber = currentNumber * 10 + digit;
[displayString appendString: [NSString stringWithFormat: @"%i", digit]];
display.text = displayString;
}
- (IBAction)clickDigit:(UIButton *)sender
{
int digit = sender.tag;
[self processDigit:digit];
}
-(void) processOp:(char)theOp
{
NSString *opString;
op = theOp;
switch (theOp) {
case '+':
opString = @"+";
break;
case '-':
opString = @"-";
break;
case '*':
opString = @"x";
break;
case '/':
opString = @"/";
break;
}
[self storeFracPArt];
firstOperand = NO;
isNumerator = YES;
[displayString appendString: opString];
display.text = displayString;
}
-(void) storeFracPArt
{
if (firstOperand) {
if (isNumerator) {
myCalculator.operand1.numerator = currentNumber;
myCalculator.operand1.denominator = 1;}
else
myCalculator.operand1.denominator = currentNumber;
}
else if (isNumerator) {
myCalculator.operand2.numerator = currentNumber;
myCalculator.operand2.denominator= 1;
}
else
{
myCalculator.operand2.denominator = currentNumber;
firstOperand = YES;
}
currentNumber = 0;
}
-(IBAction)clickOver
{
[self storeFracPArt];
isNumerator = NO;
[displayString appendString: @"|"];
display.text = displayString;
}
//arithmetic operations
-(IBAction) clickPlus
{
[self processOp: '+'];
}
-(IBAction)clickMinus
{
[self processOp: '-'];
}
-(IBAction)clickMultiply
{
[self processOp: '*'];
}
-(IBAction)clickDivide
{
[self processOp: '/'];
}
//misc buttons
-(IBAction)clickEquals
{
if ( firstOperand == NO) {
//Calculator *myCalculator = [[Calculator alloc]init];
[self storeFracPArt];
[myCalculator performOperation: op];
[displayString appendString: @"="];
[displayString appendString: [myCalculator.accumulator convertToString]];
display.text=displayString;
currentNumber=0;
isNumerator=YES;
firstOperand=YES;
[displayString setString:@""];
}
}
-(IBAction)clickClear
{
isNumerator= YES;
firstOperand = YES;
currentNumber = 0;
[myCalculator clear];
[displayString setString:@""];
display.text=displayString;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
when I run this in the iOS simulator and click the equals button I get the following exception:
'NSInvalidArgumentException', reason: '-[__NSCFString appendString:]: nil argument'
for the following line of code
Code:
[displayString appendString: [myCalculator.accumulator convertToString]];
Ok, so I thought "The myCalculator" reference must not be instantiated and is set to nil.
So, I instantiate it in "clickEquals" (I get a warning that says I'm masking my instance variable (though Xcode won't let me instantiate that one)) and it works. It clears up the exception.
BUT there is a bit of a scope problem.
So I need some help.