cs193p – Project #1 Assignment #1 Task #4

By Creator:Sebastian C. Adams [Public domain], via Wikimedia Commons

Please note, this blog entry is from a previous course. You might want to check out the current one.

Add a UILabel to your UI which shows a history of every operand and operation input by the user. Place it at an appropriate location in your UI.

Add the UILabel at the top of the Storyboard, align it with the left and top guard and resize it to the right hand side guard. Align the display label with the bottom of the new label and remove its top constraint. Add a vertical constraint between the labels, and the missing suggested constraints for the new label. Finally repair the changed constraints for the buttons allowing to “move” their frames.

cs193p - Project #1 Assignment #1 Task #4 - Add UILabel and Adjust AutoLayout
cs193p – Project #1 Assignment #1 Task #4 – Add UILabel and Adjust AutoLayout

Set the label to truncate head to show only the last entries, when there are to many to display …

… and create a outlet for the new label in the view-controller class:

    @IBOutlet weak var history: UILabel!

For the calculator brain add a new method to provide a string of the current operations and operands in the stack. To do that map over the stack to create a new array of strings holding the descriptions of the stack entries, and join them using a space character as separator:

    func showStack() -> String? {
        return " ".join(opStack.map{ "\($0)" })

Call the new method when we set the display value in the view-controller class:

    var displayValue: Double {
        set {
            history.text = brain.showStack()

The complete code for the task #4 is available on GitHub.


10 thoughts on “cs193p – Project #1 Assignment #1 Task #4”

  1. I am trying this part without the MVC ie there is no calculator brain. So I am a bit confused on the showStack part. I tried adding a label, creating an outlet and then within the dispayValue adding history.text=showStack () –> as I don’t have a brain yet. Now when I do the func showstack it comes out blank.

    1. If you have no calculator brain, which is access by my showStack implementation of the controller, you need to adjust your implementation to provide the content of the stored data/operations from where ever you store it … Do you have a link to a github repository of your project? It might be helpful to understand how you save your data/operations …

      1. I am going a bit slow with this course as I have never really programmed in my life 🙂 So sorry still getting used to github. I was able to achieve this by creating a separate function and then shoving inputs — Calcscreen.text and operand (including enter) one by one. Then I am using Literalsearch to replace the enters with the given operation. Its a bit crude but looks ok.
        Btw, strangely I received no notification of a new comment. I would have checked in sooner.

        1. If it works for you, that’s perfect. In programming there is never a single perfect solution – even if people like to argue about it.

          Most important is that the code does what is supposed to do, without unintended side effects. … and that after some time, you or even somebody else is able to make sense of it …

          There are concepts to produce less error-prone code. One of these – also called patterns – is MVC. Patterns are concepts which have proven to be effective … It it’s not really necessary or useful to invent the wheel again and again …

  2. They changed a bit the order to make it more readable:

    return opStack.map{ “\($0)” }.joinWithSeparator(” “)

    work properly.

Leave a Reply

Your email address will not be published.