Please note, this blog entry is from a previous course. You might want to check out the current one.
When the user hits an operation button, put an = on the end of the UILabel you added in the Required Task above. Thus the user will be able to tell whether the number in the Calculator’s display is the result of a calculation or a number that the user has just entered. Don’t end up with multiple occurrences of = in your UILabel.
When the display value is set – which is basically every time an operation button is pressed – add the “=” to the history label but only if there is something to show:
var displayValue: Double { ... let stack = brain.showStack() if !stack!.isEmpty { history.text = stack! + " =" } ... }
When a new number is entered show the stack without equation sign:
@IBAction func appendDigit(sender: UIButton) { ... else { ... history.text = brain.showStack() } }
Finally also reset the history label:
@IBAction func clear() { ... history.text = "" }
The complete code for extra task #2 is available on GitHub.
I think that it would be better if instead of —
var displayValue: Double {
...
let stack = brain.showStack()
if !stack!.isEmpty {
history.text = stack! + " ="
}
...
}
we use —
var displayValue: Double {
...
if let stack = brain.showStack() {
if !stack!.isEmpty {
history.text = stack! + " ="
}
}
...
}
true
The statement:
if let stack = brain.showStack() {….
is very appropriate.
After that, the tipe of stack is String, not optional String. Therefore the successive postfix exclamation points are not needed.
but where is difference in history between enterButton and operate button?
Should there be one? Both should fire up the calculation.
Hi!
I do not understand the proposed solution.
Is supposed to display only the symbol “=” when an operation is performed.
This is what I’ve done and it seems to work, but I’m newbie and may be too easy:
@IBAction func operate(sender: UIButton) {
if userIsInTheMiddleOfTypingANumber {
enter()
}
if let operation = sender.currentTitle {
if let result = brain.performOperation(operation) {
displayValue = result
history.text = brain.showTrack()! + ” =”
} else {
displayValue = 0
}
}
}
My solution is ONE possible solution out if many! If your solutions works and you are more comfortable to use it, that’s perfect!
My solution has only one line of code, and is more appropriate given the task:
@IBAction func operate(sender: UIButton) {
if userIsInTheMiddleOfTypingANumber {
enter()
}
if let operation = sender.currentTitle {
if let result = brain.performOperation(operation) {
displayValue = result
} else {
displayValue = nil
}
}
history.text = history.text! + "=" // All you need
}
My solution is ONE possible solution out of many! If your solutions works and you are more comfortable to use it, that’s perfect!
I think you made a small mistake Patrick. history.text! += ” =” should have been inside the “if let result = brain.performOperation(operation) {}” loop as otherwise it will show “=” even when wrong/ inadmissible operation is entered by user.