I was scouring the web for blogs and videos about programming in Swift I found a couple that were showing how to build a calculator. I decided to build one of my own to help me in my swift discovering journey.

If you need the Xcode project files you can download them here.

##How it works

It’s a basic calculator, nothing fancy.  The app performs the usual arithmetic operations plus a series specialized percentage buttons intended to be used as tip calculators. I personally tested it on my iPhone 5 and it works as intended.

I created the following user stories to guide me through the development:

1. I need to be able to perform the basic arithmetic operations +,-,* and /
2. I need to be able to perform operations with non integer numbers
3. As a  user every time I hit the = sign the operation is performed and the result is shown on the display
4. As a user I need to be able to perform an operation on a result displayed
5. I need to be able to clear the screen without clear the memory
6. I need to be able to clear the memory
7. As a user I need to have specialized "tip" calculator button at 10%, 15%, 18%, and 20%.
1. When I hit those button the resulting percentage in shown on the screen
2. Every percentage needs to perform an independent calculation

## Creating the app in XCode 6

As in every iOS application we need to start in Xcode 6 as a brand new application:

1. Select a single view application
2. In the following screen select "Swift" as a language
3. We do not need "core data" so leave it uncheck
4. To keep it simple I made the app for iPhone only

## The Storyboard

I kept the calculator’s look and feel is very simple (see Fig. 3). I created all the numbers buttons, the Clear and All Clear buttons, and all the standard arithmetic operations plus additional specialized “tip” buttons.

• The digits 0-9 and the decimal (.) are connected to the IBAction "digitTapped"
• The buttons AC  is connected to theIBAction "allClearTapped"
• The buttons C  is connected to the IBAction "clearScreenTapped"
• The operands buttons +, -, x, and / are connected to the IBAction "operationTapped"
• The buttons with 10%, 15%, 18% and 20% are connected to the IBAction "percentageTapped"
• The equal (=) button is connected to the IBAction "equalTapped"

The way I organized the layout is explained in Fig.4  and is as follows:

• (1) Is the main label which is named answerFieldLabel.  The results are displayed here
• (2) This is a hidden label named operandLabel in which keeps track of the operand the user has chosen at any given time (+,-,x, /)
• (3) All Clear and Clear buttons. The AC resets all the variables and arrays, the C only resets the answerFieldLabel
• (4) This is the percentage or "tip" button and performs a percentage calculation based on the result saved
• (5) The operations buttons: addition, subtraction, multiplication and division
• (6) The equal button that performs the operations
• (7) The keypad including the decimal dot (.)

##The Classes I create two classes for this project: ArithmeticOperation and tipCalculator.

The main class for the Calculator is the ArithmeticOperation class which returns the value of the operation requested. Each operation is a method (addition, subtraction, multiplication and division).

All the methods are pretty straight forward. For the division() method I implemented a failsafe to avoid a division by zero operation.

For the percentage or “tip” calculator I created a separate class called tipCalculator class that has a method called calculatedTip()  that returns the percentage chosen.

You can try out all the classes by cutting and pasting the code in the Xcode playground:

##The Code The four most important functions are digitTapped(),  percentageTapped()equalTapped(), and operatorTapped(). I use an array called resultArray to keep track of the result. Also I have two variables rNumber and lNumber with the values of the right and left numbers that are needed for an arithmetic operation as in ”rNumber + lNumber = result”. The variable “op” represent the arithmetic operand chosen and it’s connected to operandLabel

This is the code in the ViewController.swift file between the brackets under “class ViewController: UIViewController”: