Please note, this blog entry is from a previous course. You might want to check out the current one.
Implement a “backspace” button for the user to touch if they hit the wrong digit button. This is not intended to be “undo,” so if the user hits the wrong operation button, he or she is out of luck! It is up to you to decide how to handle the case where the user backspaces away the entire number they are in the middle of typing, but having the display go completely blank is probably not very user-friendly. You will find the Strings and Characters section of the Swift Reference Guide to be very helpful here.
Add the back-space button (or two) in the interface builder:
Link the new button(s) to a new action method. When the user is in the middle of typing remove the last digit. It it was the last one set the display to zero:
@IBAction func backSpace(sender: UIButton) { if userIsInTheMiddleOfTyping { if var text = display.text { text.removeAtIndex(text.endIndex.predecessor()) if text.isEmpty { text = "0" userIsInTheMiddleOfTyping = false } display.text = text } } }
The complete code for the extra task #1 is available on GitHub.