Please note, this blog entry is from a previous course. You might want to check out the current one.
When you touch the C button, the M variable should be removed from the variableValues Dictionary in the CalculatorBrain (not set to zero or any other value). This will allow you to test the case of an “unset” variable (because it will make evaluate() return nil and thus your Calculator’s display will be empty if M is ever used without a →M).
… well … we (I) just reset the whole calculator brain by throwing away the old brain and creating a new one. Thus, also the variable values are reset by default. Maybe that was not the intended way to do …
If you have another better solution, please post them in the comments section!
brain.variableValues.removeAll(keepCapacity: false)
seems to work
Yes, but when you nullify the brain, the variables are reset without anything to do …
My solution is slightly different and doesn’t require to nullify the brain as the brain may have trained certain Ops using knownOps.
In Brain I have a reset method:
func reset() {
opStack = []
vaiableVaules[] = [:]
}
In Controller ..I set the display and historyLabel to empty space and call the above reset function:
@IBAction func clear() {
displayValue = nil
history.text = ” ”
brain.reset()
}
… the task asks to remove all contents of the brain …
But what is the code to nullify the brain?
Never mind, I figured out how to reset the brain. Just do:
self.brain = CalculatorBrain()