This is part 2 of my Learning Swift Calculator App.
Progress:
- +, -, x operations are now functional
- decimals also work
Lessons learned:
- It’s easy to get caught up on a new programming language and forget to use universal concepts of programming that would have made my life a lot easier
- In my haste (and learning/programming style), I jumped in with both feet, and soon I had one too many else if statements. Â I cleaned up the initial logic to use case statements instead. Â Not different than any other programming language out there – just common sense and cleaner programming all around.
- Layout issues – for the first few versions, I will stick with programming for a target device (iPhone5) instead of universal design. Â This can be enabled on the view controller file inspector > disable “Use Auto Layout” (thanks Dr. Hooper for pointing me to this setting!)
- Calculators are very complicated!
- My initial versions will ignore order of operations
- Will not check user input for errors (for example, if the user presses + +, it will give unexpected results)
- I have some issues with decimals (1+1.1=2.0999….) – need to check into how my values are being saved. Â Again variable types seem to be haunting me.
- Break points are super easy to use! Â (Apple+”\” to add/delete) Â The debug interface is intuitive and clearly shows all the variables. Â The debug navigator (6th icon on the 2nd row of icons on the top left… looks like a line, 3 columns, and a line) is also a great snapshot into what’s going on – shows cpu/memory/disk/network usage.
Code as of this writing for ViewController.swift:
//
// ViewController.swift
// Calculator
//
// Created by Jin S. An on 10/1/14.
// Copyright (c) 2014 jinsungpsu.com. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var displayString = ""
@IBOutlet var calcDisplay: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func updateCalculatorDisplay(buttonPressed: String) {
switch (buttonPressed) {
case ".","1","2","3","4","5","6","7","8","9","0":
displayString += buttonPressed
case "-","+","x":
displayString += " " + buttonPressed + " "
default:
displayString += buttonPressed
}
calcDisplay.text = displayString
}
@IBAction func calc01(sender: AnyObject) {
updateCalculatorDisplay("1")
}
@IBAction func calc02(sender: AnyObject) {
updateCalculatorDisplay("2")
}
@IBAction func calc03(sender: AnyObject) {
updateCalculatorDisplay("3")
}
@IBAction func calc04(sender: AnyObject) {
updateCalculatorDisplay("4")
}
@IBAction func calc05(sender: AnyObject) {
updateCalculatorDisplay("5")
}
@IBAction func calc06(sender: AnyObject) {
updateCalculatorDisplay("6")
}
@IBAction func calc07(sender: AnyObject) {
updateCalculatorDisplay("7")
}
@IBAction func calc08(sender: AnyObject) {
updateCalculatorDisplay("8")
}
@IBAction func calc09(sender: AnyObject) {
updateCalculatorDisplay("9")
}
@IBAction func calcDecimal(sender: AnyObject) {
updateCalculatorDisplay(".")
}
@IBAction func calc00(sender: AnyObject) {
updateCalculatorDisplay("0")
}
@IBAction func calcPlus(sender: AnyObject) {
updateCalculatorDisplay("+")
}
@IBAction func calcMinus(sender: AnyObject) {
updateCalculatorDisplay("-")
}
@IBAction func calcMultiply(sender: AnyObject) {
updateCalculatorDisplay("x")
}
@IBAction func calcEqual(sender: AnyObject) {
var tempArray = displayString.componentsSeparatedByString(" ")
var currentOperation = "initial"
var calculatedValue:Float = 0.0;
for tempValue in tempArray {
switch (tempValue) {
case "+":
currentOperation = "+"
case "-":
currentOperation = "-"
case "x":
currentOperation = "x"
case "%":
currentOperation = "%"
default:
switch (currentOperation) {
case "+":
calculatedValue += (tempValue as NSString).floatValue
case "-":
calculatedValue -= (tempValue as NSString).floatValue
case "x":
calculatedValue *= (tempValue as NSString).floatValue
case "initial":
calculatedValue = (tempValue as NSString).floatValue
default:
calculatedValue += 0.0
}
}
}
displayString = "=" + calculatedValue.description
calcDisplay.text = displayString
}
@IBAction func calcClear(sender: AnyObject) {
displayString = ""
calcDisplay.text = ""
}
}