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!
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"
}
}
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"
}
}
squareRootPressed
action.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 = ""
}
}
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!