Last active
March 23, 2017 18:31
-
-
Save soemarko/334bf6d4eda1513a250a416de98de91a to your computer and use it in GitHub Desktop.
Dead simple calculator in Swift 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Soemarko Ridwan on 3/23/17. | |
// Copyright © 2017 Soemarko Ridwan. All rights reserved. | |
// | |
// Dependencies: (to simplify the code) | |
// - ActionKit: https://github.com/ActionKit/ActionKit/tree/swift3 | |
// For closure style buttons | |
// - AutoLayout Helper: https://github.com/ustwo/autolayout-helper-swift | |
// I was never a fan of interface builder, a lot more across devices. | |
// | |
// Photo: https://tmblr.co/ZBVAFy2Js5KA6 | |
// | |
import UIKit | |
class aViewController: UIViewController, UITextFieldDelegate { | |
var field: UITextField? = nil | |
var operand: String? = "" | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
let wrapper = UIView() | |
self.view.addSubview(wrapper) | |
wrapper.addTopConstraint(toView: self.view, constant:80.0) | |
wrapper.addLeftConstraint(toView: self.view, constant:10.0) | |
wrapper.addRightConstraint(toView: self.view, constant:-10.0) | |
wrapper.addHeightConstraint(toView: nil, constant: 40.0) | |
wrapper.backgroundColor = UIColor(white: 0.85, alpha: 0.9) | |
field = UITextField() | |
wrapper.addSubview(field!) | |
field?.fillSuperView(UIEdgeInsetsMake(5, 10, 5, 10)) | |
field?.keyboardType = .decimalPad | |
let toolBar = UIToolbar() | |
toolBar.barStyle = .default | |
toolBar.isTranslucent = true | |
let clearButton = UIBarButtonItem(title: "clear") { | |
// clear | |
self.field?.text = "" | |
self.operand = "" | |
} | |
let addButton = UIBarButtonItem(title: "+") { | |
// add | |
if self.operand != "" { | |
let result = self.compute(string: (self.field?.text)!, operand: self.operand!) | |
self.field?.text = String(result) | |
} | |
self.operand = "+" | |
self.field?.text?.append("+") | |
} | |
let subtractButton = UIBarButtonItem(title: "-") { | |
// sub | |
if self.operand != "" { | |
let result = self.compute(string: (self.field?.text)!, operand: self.operand!) | |
self.field?.text = String(result) | |
} | |
self.operand = "-" | |
self.field?.text?.append("-") | |
} | |
let multiplyButton = UIBarButtonItem(title: "*") { | |
// mul | |
if self.operand != "" { | |
let result = self.compute(string: (self.field?.text)!, operand: self.operand!) | |
self.field?.text = String(result) | |
} | |
self.operand = "*" | |
self.field?.text?.append("*") | |
} | |
let divideButton = UIBarButtonItem(title: "/") { | |
// div | |
if self.operand != "" { | |
let result = self.compute(string: (self.field?.text)!, operand: self.operand!) | |
self.field?.text = String(result) | |
} | |
self.operand = "/" | |
self.field?.text?.append("/") | |
} | |
let equalButton = UIBarButtonItem(title: "=") { | |
// equal | |
if self.operand != "" { | |
let result = self.compute(string: (self.field?.text)!, operand: self.operand!) | |
self.field?.text = String(result) | |
} | |
self.operand = "" | |
} | |
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) | |
let fixedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil) | |
fixedSpace.width = 20.0 | |
toolBar.setItems([clearButton, flexSpace, addButton, fixedSpace, subtractButton, fixedSpace, multiplyButton, fixedSpace, divideButton, fixedSpace, equalButton, fixedSpace], animated: false) | |
toolBar.isUserInteractionEnabled = true | |
toolBar.sizeToFit() | |
field?.inputAccessoryView = toolBar | |
field?.delegate = self | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
field?.becomeFirstResponder() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { | |
let char = string.cString(using: String.Encoding.utf8)! | |
let isBackSpace = strcmp(char, "\\b") | |
if isBackSpace == -92 { | |
if (textField.text?.hasSuffix("+"))! || | |
(textField.text?.hasSuffix("-"))! || | |
(textField.text?.hasSuffix("*"))! || | |
(textField.text?.hasSuffix("/"))! { | |
self.operand = "" | |
print("remove operand") | |
} | |
} | |
return true | |
} | |
func compute(string: String, operand: String) -> Double { | |
let comps = string.components(separatedBy: operand) | |
if comps.count == 0 { | |
return 0 | |
} | |
else if comps.count == 1 { | |
return Double(comps[0])! | |
} | |
let left = Double(comps[0]) ?? 0.0 | |
let right = Double(comps[1]) ?? 0.0 | |
switch operand { | |
case "+": | |
return left + right | |
case "-": | |
return left - right | |
case "*": | |
return left * right | |
case "/": | |
return left / right | |
default: | |
return 0 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment