Calculator Tutorial in iOS8 with Swift

In this tutorial we create a calculator, which can add and subtract. To keep things simple, this calculator can only handle integers. This tutorial is written in Swift so you will need Xcode 6. It can be downloaded at Apple's developer portal.

Update: Apr 22, 2015. This tutorial is updated for Swift 1.2 and Xcode 6.3.

Open Xcode and create a new Single View Application. For product name, use iOS8SwiftCalculatorTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Go to the Storyboard and change the width to compact. This will change the view to a iPhone portrait mode. 

Add a label to the top of the main view. In the property inspector select "Right Alignment" and give the label a title of "0". Select the assistant editor and open the ViewController.swift file. Ctrl and drag from the button to the class section and create the following outlet.

Next, Add a button to the view and give it a title of "1". Ctrl and Drag from the button to just below the IBoutlet to create the following action

Next, copy the button in the storyboard and paste it to create alle other digits. Note this will also copy the IBAction connection. The storyboard should look like this

Drag a new button to the view. Give the button the minus sign. Ctrl+Click and Drag from the plus-button to the ViewController.swift file below the numberTapped action and create the action

Copy the plus-button and paste a new button beneath this button. Give the new button a plus sign.

Finally, create a new button below the "0" button and give it a "=" sign. Ctrl and drag from this button to the ViewController.swift file below the calculationTapped action to create the action

The final storyboard should look like this.

Now it's time to code inside our ViewController.swift file. First we will define some properties right after the "class ViewController: UIViewController {" line.

var isTypingNumber = false
var firstNumber = 0
var secondNumber = 0
var operation = ""

The isTypingNumber is a boolean to check whether we are typing a number or pressing an operation. Next, implement the numberTapped method.

@IBAction func numberTapped(sender: AnyObject) {
    var number = sender.currentTitle
    if isTypingNumber {
        calculatorDisplay.text = calculatorDisplay.text + number
    } else {
        calculatorDisplay.text = number
        isTypingNumber = true

We will append the pressed digit to the text of the calculatorDisplay label, if it is the first digit we will assign the digit to the label. Next, implement the calculationTapped method.

@IBAction func calculationTapped(sender: AnyObject) {
    isTypingNumber = false
    firstNumber = calculatorDisplay.text.toInt()!
    operation = sender.currentTitle

The text of the calculatorDisplay is converted to a integer using the toInt method from the String class. Note this function return an optional integer, because a string can be empty. In this case we are certain that the string has always a value, so we add an exclamation mark to unwrap the value. Finally, implement the equalsTapped method.

@IBAction func equalsTapped(sender: AnyObject) {
    isTypingNumber = false
    var result = 0
    secondNumber = calculatorDisplay.text.toInt()!
    if operation == "+" {
        result = firstNumber + secondNumber
    } else if operation == "-" {
        result = firstNumber - secondNumber
    calculatorDisplay.text = "\(result)"

The calculation is performed based on the value of the operation property and the result is written to the calculatorDisplay label. Build and Run the project and make some calculations.

You can download the source code of the IOS8SwiftCalculator at the ioscreator repository on github.