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:
- I need to be able to perform the basic arithmetic operations +,-,* and /
- I need to be able to perform operations with non integer numbers
- As a user every time I hit the = sign the operation is performed and the result is shown on the display
- As a user I need to be able to perform an operation on a result displayed
- I need to be able to clear the screen without clear the memory
- I need to be able to clear the memory
- As a user I need to have specialized "tip" calculator button at 10%, 15%, 18%, and 20%.
- When I hit those button the resulting percentage in shown on the screen
- 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:
- Select a single view application
- In the following screen select "Swift" as a language
- We do not need "core data" so leave it uncheck
- To keep it simple I made the app for iPhone only
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”: