-1

I'm learning Swift, and I decided that instead of always copying code from videos, I would try to build something myself.

I tried to make a calculator in a Playground to see if I could figure things out, and I came up with this. Is this a good way to do it or does anyone have any feedback before I start the Xcode project?

var result: String = ""

var plusClicked = false
var equalsClicked = false

var firstNumber: Int = 0
var secondNumber: Int = 0

func clickNumber(number: Int) {
    if equalsClicked == true {
        equalsClicked = false
        result = "" 
    }
    
    if plusClicked == true {    
        firstNumber = Int(result)!
        result = "\(number)"       
    } else {    
        result = result + "\(number)"  
    }
}

func clickPlus() {
    plusClicked = true
}

func clickEquals() {
    secondNumber = Int(result)!
    
    if plusClicked == true {
        result = "\(firstNumber + secondNumber)"
        plusClicked = false
    }
    
    equalsClicked = true
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • if equalsClicked == true { is redundant. Just change it to if equalsClicked { ... } – Leo Dabus Nov 24 '15 at 04:04
  • I'm voting to close this question as off-topic because totally off-topic. – Fattie Oct 09 '16 at 16:04
  • 1
    Don't deface the question. If you want it deleted, _you_ delete it. Or flag for a moderator. But do not misuse the question field to make a comment about the quality of the question. – matt Dec 24 '21 at 20:04
  • You should have a specific question about this code. If you want people to help you work on it, you need something more robust, like a git repository that we can use to comment and code review. – benc Feb 27 '22 at 06:32

2 Answers2

-2

First of all, playgrounds generally work a little differently than full-blown apps. They are great for testing things out, but not very good when it comes to relatively complex business logic.

A calculator can be done in a variety of ways. The one you chose is particularly convoluted, and quite muddy. You seem to be doing a lot of useless things. The very first thing that struck me was all those strings. Why rely on strings so much if you are dealing with numbers? Furthermore, the general approach you adopted is not very flexible. I get that you only implemented addition, but you seem to assume that you will only ever sum two numbers together. In reality, you may want to chain-sum several before hitting "equals" and get the grand total.

Now, if your goal is to learn Swift as a language, (almost) any approach is good. However, here I suspect you need to learn a little more than merely the language – but then again I don't know you and I might be completely wrong.

Morpheu5
  • 2,610
  • 6
  • 39
  • 72
  • I don't mean to be rude, but even the apprach you adopted for the sum here cries for some deep re-thinking :) – Morpheu5 Nov 25 '15 at 09:18
-2

I just started learning swift and the first few days I tried to read every book I could find and watch every video tutorial. But I found out (so far) from what I have learned the most from has been learning from building an app, which I just keep trying to make more advanced step by step, and learning from each of those steps. I sat down, took some paper and designed how my app should be when its final, with bunch of advanced function, which i'm not sure I will ever understand how to create. But I already has gotten a lot further that I thought I would, as you'll learn so much when you hit a obstacle in the next step and you try to solve it.

So figure out an app you want and dont set the bar to low actually! thats my advice!

Frederic
  • 497
  • 2
  • 9
  • 22