0

I just started the Stanford iOS5 class and have completed the RPN calculator, but want to make the "Clear" button work a little differently. This is how I have it now, with no changes to the CalculatorModel "brain":

- (IBAction)clearPressed
{
    self.display.text = @"";                        // Clear the display
    self.historyWindow.text = @"";                  // Clear the history window
    self.model = nil;                               // Reset the stack
    self.userIsInMiddleOfEnteringNumber = NO;       // Reset user typing boolean
}

I may be wrong, but "self.model = nil;" seems like it doesn't really remove the objects from the stack, it kind of just imitates that. So I added a function to the CalculatorModel "brain":

-(void) clearOperandStack
{
    [self.operandStack removeAllObjects];
}

and want to call it in my "clearPressed" function in the CalculatorViewController but I am having issues possibly because I do not fully understand objective c yet. This is what I thought I had to do, but it does not seem to want to work.

- (IBAction)clearPressed
{
    self.display.text = @"";                        // Clear the display
    self.historyWindow.text = @"";                  // Clear the history window
//    self.model = nil;                               // Reset the stack
    [self.model clearOperandStack];
    self.userIsInMiddleOfEnteringNumber = NO;       // Reset user typing boolean
}

Can someone please explain to me the proper way to call that method / what I am doing wrong?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Joey
  • 1,144
  • 3
  • 15
  • 29
  • Did you add the declaration to the brain's .h file as well? – rooftop Feb 22 '12 at 02:40
  • _does not seem to want to work_ -- do you get compile errors? runtime errors? What does it do? What did you expect it to do? – sarnold Feb 22 '12 at 03:02
  • rooftop, you were right. I did not add the declaration in the .h file. I'm guessing if you add it to the implementation file and not the interface it is considered a private function? Thanks for the help! – Joey Feb 22 '12 at 04:21
  • Yes, the implementation file is private API. – MaxGabriel Feb 22 '12 at 13:41

2 Answers2

1

Here is a link to another thread that answers the question.

CS193P - Adding cancel button to iOS calculator

Lets see if I can explain why the self.model = nil;

I'm going to use an analogy of a sheet of paper for your model. On this paper you could write your stack, plus any additional things your model may know about.

Assuming you lazily instantiated your getter for the model (like the instructor did); When you call self.model, if you already have a sheet of paper that has your model it will give you that, if you do not have a sheet of paper for your model, it will grab a blank piece of paper and make it your model.

Now when you say self.model = nil; you are telling it to throw away your models sheet of paper. This means you now longer have a stack, or any of the additional things that are in your model.

Now with the getter being lazily instantiated, when you ask for your model next time it doesn't already have one, so it will make a brand new one for you.

So maybe a quick summary is self.model = nil; doesn't empty your stack, it completely throws the stack away, you don't have an empty stack, you have no stack.

Whereas your clearOperandsStack, just empties the stack, so that you have an empty stack.

Community
  • 1
  • 1
Joe_Schmoe
  • 1,485
  • 3
  • 17
  • 19
  • Thank you for the clear explanation @Joe_Schmoe. Is there any convention on which is better practice? To throw away the stack or just clear it? – Joey Feb 26 '12 at 01:37
0

My solution of assignment 1: https://github.com/rl1987/CS193p-Homework-1

user1044147
  • 108
  • 1
  • 6