Simple Calculator App Examples in Swift

Explore practical examples of creating a simple calculator app in Swift, perfect for beginners to enhance their programming skills.
By Taylor

Introduction

Creating a simple calculator app in Swift is a fantastic way to get started with iOS development. This project will help you grasp the basics of building user interfaces and working with user inputs. In this article, we’ll walk through three diverse examples that demonstrate different functionalities of a calculator app, making coding accessible and fun!

Example 1: Basic Arithmetic Calculator

Context

This example covers the fundamental operations of addition, subtraction, multiplication, and division. It’s a great starting point for beginners to learn about user interactions and basic arithmetic logic.

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var displayLabel: UILabel!
    var currentInput: String = ""
    var previousInput: String = ""
    var operation: String = ""

    @IBAction func numberPressed(_ sender: UIButton) {
        guard let number = sender.titleLabel?.text else { return }
        currentInput += number
        displayLabel.text = currentInput
    }

    @IBAction func operationPressed(_ sender: UIButton) {
        previousInput = currentInput
        currentInput = ""
        operation = sender.titleLabel?.text ?? ""
    }

    @IBAction func equalsPressed(_ sender: UIButton) {
        let result: Double
        let previous = Double(previousInput) ?? 0
        let current = Double(currentInput) ?? 0

        switch operation {
        case "+": result = previous + current
        case "-": result = previous - current
        case "×": result = previous * current
        case "÷": result = previous / current
        default: return
        }
        displayLabel.text = String(result)
        currentInput = ""
    }

    @IBAction func clearPressed(_ sender: UIButton) {
        currentInput = ""
        previousInput = ""
        displayLabel.text = "0"
    }
}

Notes

  • Ensure your storyboard has buttons connected to the respective IBOutlets and IBActions.
  • You can customize the design by adding colors and shapes to your buttons for a better UI experience.

Example 2: Scientific Calculator with Square Root

Context

This example expands on the basic calculator by introducing a square root function, which is common in scientific calculators. Ideal for users looking to perform more advanced calculations.

import UIKit

class ScientificCalculatorViewController: UIViewController {
    @IBOutlet weak var displayLabel: UILabel!
    var currentInput: String = ""

    @IBAction func numberPressed(_ sender: UIButton) {
        guard let number = sender.titleLabel?.text else { return }
        currentInput += number
        displayLabel.text = currentInput
    }

    @IBAction func squareRootPressed(_ sender: UIButton) {
        let number = Double(currentInput) ?? 0
        let result = sqrt(number)
        displayLabel.text = String(result)
        currentInput = ""
    }

    @IBAction func clearPressed(_ sender: UIButton) {
        currentInput = ""
        displayLabel.text = "0"
    }
}

Notes

  • For this example, ensure you have a button for the square root function, linked to the squareRootPressed action.
  • You can enhance user experience by adding error handling for negative inputs when calculating the square root.

Example 3: Currency Converter Calculator

Context

This example illustrates how to create a simple currency converter within a calculator app. This is perfect for users who want to perform currency conversions along with basic arithmetic calculations.

import UIKit

class CurrencyConverterViewController: UIViewController {
    @IBOutlet weak var amountInput: UITextField!
    @IBOutlet weak var resultLabel: UILabel!

    @IBAction func convertPressed(_ sender: UIButton) {
        let amount = Double(amountInput.text ?? "0") ?? 0
        let exchangeRate = 1.2 // Example rate from USD to EUR
        let convertedAmount = amount * exchangeRate
        resultLabel.text = String(format: "%.2f EUR", convertedAmount)
    }

    @IBAction func clearPressed(_ sender: UIButton) {
        amountInput.text = ""
        resultLabel.text = "" 
    }
}

Notes

  • You can enhance this example by adding more currencies and allowing users to select source and target currencies.
  • Consider using real-time exchange rates by integrating an API for live conversions.

Conclusion

These examples of creating a simple calculator app in Swift provide a foundation for beginners to build on. Whether it’s basic arithmetic, scientific calculations, or currency conversions, each example enhances your understanding of Swift and iOS development. Happy coding!