How to show thousands grouping in a Swift calculator? - ios

I have code for my simple calculator.
How can I make the division of thousands so that when you enter numbers they are automatically separated by a dot?
For example:
Before:
1000000
After:
1.000.000
Code for calculator:
var stillTyping = false
var dotIsPlaced = false
var firstOperand: Double = 0
var secondOperand: Double = 0
var operationSign: String = ""
var currentInput: Double {
get {
return Double (displayResultLabel.text!)!
}
set {
let value = "\(newValue)"
let ValueArray = (value.components(separatedBy:"."))
if ValueArray[1] == "0" {
displayResultLabel.text = "\(ValueArray[0])"
} else {
displayResultLabel.text = "\(newValue)"
}
stillTyping = false
}
}
#IBAction func numberPressed(_ sender: UIButton) {
let number = sender.currentTitle!
if stillTyping {
if (displayResultLabel.text?.characters.count)! < 14 {
displayResultLabel.text = displayResultLabel.text! + number
}
} else {
displayResultLabel.text = number
stillTyping = true
}
}
#IBAction func twoOperandsSignPressed(sender: UIButton) {
operationSign = sender.currentTitle!
firstOperand = currentInput
stillTyping = false
dotIsPlaced = false
}
func operateWithTwoOperands(operation: (Double, Double) -> Double) {
currentInput = operation(firstOperand, secondOperand)
stillTyping = false
}

You should use this extension, which works with NumberFormatter:
struct Number {
static let formatterWithDots: NumberFormatter = {
let formatter = NumberFormatter()
formatter.groupingSeparator = "." // only include this if you want to use dots regardless of the locale
formatter.numberStyle = .decimal
return formatter
}()
}
extension Double {
var formattedWithDots: String? {
return Number.formatterWithDots.string(from: self as NSNumber)
}
}
Usage:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChange), name: .UITextFieldTextDidChange, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
#objc fileprivate func textFieldDidChange() {
if let number = Double(textField.text ?? "") {
textField.text = number.formattedWithDots
}
}

Related

How to manage counting priority calculation in a calculator app ? (Swift)

I'm doing a calculator for school I finished everything but in the end the priority of calculation is not respected for example when I do : 2 + 2 * 2 it should be 6 but in my app it tells me 8, anyone have an idea how can I do that ? you will find in my code my model and my controller :
class viewControllerUtilities: UIViewController {
var stringNumbers: [String] = [String()]
var operators: [String] = ["+"]
var formerResult: Double?
var index = 0
var isExpressionCorrect: Bool{
if let stringNumber = stringNumbers.last{
if stringNumber.isEmpty{
if stringNumbers.count == 1 {
return false
}
return false
}
}
return true
}
var canAddOperator: Bool {
if let stringNumber = stringNumbers.last{
if stringNumber.isEmpty && formerResult == nil{
return false
}
}
return true
}
var canAddDecimal: Bool{
if let strings = stringNumbers.last{
if strings.contains(".") || strings.isEmpty{
return false
}
}
return true
}
func addDecimal(){
if let stringNumber = stringNumbers.last{
var stringNumberDecimal = stringNumber
stringNumberDecimal += "."
stringNumbers[stringNumbers.count-1] = stringNumberDecimal
}
}
func calculateTotal() -> Double{
var total : Double = 0
for (i, stringNumber) in stringNumbers.enumerated(){
if let number = Double(stringNumber){
switch operators[i]{
case "+":
total += number
case "-":
total -= number
case "x":
total *= number
case "/":
total /= number
default:
break
}
}
}
formerResult = total
clear()
return total
}
func clear(){
stringNumbers = [String()]
operators = ["+"]
index = 0
}
func allClear(){
clear()
formerResult = nil
}
func sendOperand(operand: String, number: String) {
operators.append(operand)
stringNumbers.append(number)
}
func addNewNumber(_ newNumber: Int){
if let stringNumber = stringNumbers.last{
var stringNumberMutable = stringNumber
stringNumberMutable += "\(newNumber)"
stringNumbers[stringNumbers.count-1] = stringNumberMutable
}
}
func roundResult(_ result: Double?){
if roundEvaluation(result!){
let rounded = Int(result!)
stringNumbers = ["\(rounded)"]
formerResult = nil
}
}
func roundEvaluation(_ result: Double) -> Bool{
if result.truncatingRemainder(dividingBy: 1) == 0{
return true
}
return false
}
}
and my controller :
class ViewController: UIViewController {
// MARK: - Properties
var CountOnMeU = viewControllerUtilities()
// MARK: - Outlets
#IBOutlet weak var textView: UITextView!
#IBOutlet var numberButtons : [UIButton]!
#IBOutlet var operators: [UIButton]!
#IBOutlet weak var point: UIButton!
// MARK: - Action
#IBAction func tappedNumberButton(_ sender: UIButton) {
for (i, numberButton) in numberButtons.enumerated() where sender == numberButton{
CountOnMeU.addNewNumber(i)
updateDisplay()
}
}
#IBAction func tappedPointButton(_ sender: Any){
if CountOnMeU.canAddDecimal{
CountOnMeU.addDecimal()
updateDisplay()
} else {
showAlert(message: "Vous ne pouvez pas mettre 2 points")
}
}
#IBAction func equal() {
if !CountOnMeU.isExpressionCorrect{
showAlert(message: "opération invalide")
} else {
let total = CountOnMeU.calculateTotal()
textView.text! += "\n =\(total)"
}
}
#IBAction func operandButtonTapped(_ sender: UIButton){
performOperation(operand: (sender.titleLabel?.text!)!)
}
#IBAction func allClear(_ sender: UIButton) {
CountOnMeU.allClear()
textView.text = "0"
}
// MARK: - Methods
func addNewNumber(message: String){
let alertVC = UIAlertController(title: "Erreur", message: message, preferredStyle: .alert)
alertVC.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alertVC, animated: true, completion: nil)
}
func updateDisplay() {
var text = ""
let stack = CountOnMeU.stringNumbers.enumerated()
for (i, stringNumber) in stack {
// Add operator
if i > 0 {
text += CountOnMeU.operators[i]
}
// Add number
text += stringNumber
}
textView.text = text
}
func showAlert(message: String){
let AlertVC = UIAlertController(title: "Erreur", message: message, preferredStyle: .alert)
AlertVC.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(AlertVC,animated: true, completion: nil)
}
func performOperation(operand: String){
if CountOnMeU.canAddOperator{
let result = CountOnMeU.formerResult
if result != nil {
CountOnMeU.roundResult(result)
updateDisplayForResultReuse(operand: operand)
} else {
CountOnMeU.sendOperand(operand: operand, number: "")
updateDisplay()}
} else {
self.showAlert(message: "Expression incorrecte")
}
}
func updateDisplayForResultReuse(operand: String){
updateDisplay()
CountOnMeU.sendOperand(operand: operand, number: "")
updateDisplay()
}
}

Change a button background on click of another button

I use this code so that when I click on a button its background changes to a picture with a white border and when I press it again it changes to a picture with a gray background (the button always has a gray background).
How can I do so that when I click on another button (clear and equal), the background of the "+", "-", "/", "*" changes to gray as it was before pressing.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var displayResultLabel: UILabel!
var stillTyping = false
var dotIsPlaced = false
var firstOperand: Double = 0
var secondOperand: Double = 0
var operationSign: String = ""
var currentInput: Double {
get {
return Double (displayResultLabel.text!)!
}
set {
let value = "\(newValue)"
let ValueArray = (value.components(separatedBy:"."))
if ValueArray[1] == "0" {
displayResultLabel.text = "\(ValueArray[0])"
} else {
displayResultLabel.text = "\(newValue)"
}
stillTyping = false
}
}
#IBAction func numberPressed(_ sender: UIButton) {
let number = sender.currentTitle!
if stillTyping {
if (displayResultLabel.text?.characters.count)! < 20 {
displayResultLabel.text = displayResultLabel.text! + number
}
} else {
displayResultLabel.text = number
stillTyping = true
}
}
#IBAction func twoOperandsSignPressed(sender: UIButton) {
operationSign = sender.currentTitle!
firstOperand = currentInput
stillTyping = false
dotIsPlaced = false
}
func operateWithTwoOperands(operation: (Double, Double) -> Double) {
currentInput = operation(firstOperand, secondOperand)
stillTyping = false
}
#IBAction func equalitySignPressed(sender: UIButton) {
if stillTyping {
secondOperand = currentInput
}
dotIsPlaced = false
switch operationSign {
case "+":
operateWithTwoOperands{$0 + $1}
case "-":
operateWithTwoOperands{$0 - $1}
case "✕":
operateWithTwoOperands{$0 * $1}
case "÷":
operateWithTwoOperands{$0 / $1}
default: break
}
}
#IBAction func clearButtonPressed(_ sender: UIButton) {
firstOperand = 0
secondOperand = 0
currentInput = 0
displayResultLabel.text = "0"
dotIsPlaced = false
operationSign = ""
}
// +,-
#IBAction func plusMinusButtonPressed(_ sender: UIButton) {
currentInput = -currentInput
}
#IBAction func percentageButtonPressed(_ sender: UIButton) {
if firstOperand == 0 {
currentInput = currentInput / 100
} else {
secondOperand = firstOperand * currentInput / 100
}
}
#IBAction func squareRootButtonPressed(_ sender: UIButton) {
currentInput = sqrt(currentInput)
}
#IBAction func dotButtonPressed(_ sender: UIButton) {
if stillTyping && !dotIsPlaced {
displayResultLabel.text = displayResultLabel.text! + "."
dotIsPlaced = true
} else if !stillTyping && !dotIsPlaced {
displayResultLabel.text = "0."
}
#IBAction func PercentAnimate(_ sender: UIButton) {
if sender.currentBackgroundImage == image_off {
sender.setBackgroundImage(Image_on, for: .normal)
} else {
sender.setBackgroundImage(image_off, for: .normal)
}
if (previousButton !== sender) {
previousButton.setBackgroundImage(image_off, for: .normal)
previousButton = sender
}
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
Here is the code you can Create border to any View, in your case it will be button.
func createBordersWithColor(color: UIColor, myView : UIView) {
myView.layer.borderWidth = 1
myView.layer.cornerRadius = 0
myView.layer.shouldRasterize = false
myView.layer.rasterizationScale = 2
myView.clipsToBounds = true
myView.layer.masksToBounds = true
let cgColor: CGColor = color.cgColor
myView.layer.borderColor = cgColor
}
You can use the above function with you code like
#IBAction func PercentAnimate(_ sender: UIButton) {
let btnCurrent : UIButton = sender as! UIButton
let btnPrevious : UIButton = previousButton as! UIButton
createBorderWithColor(color : UIColor.clear , myView : btnPrevious)
createBorderWithColor(color : UIColor.clear , myView : btnCurrent)
if (previousButton !== sender) {
previousButton = sender
}
}
let know if it helps, or you need any explanation.
If you want to set with image
#IBAction func PercentAnimate(_ sender: UIButton) {
let btnCurrent : UIButton = sender as! UIButton
let btnPrevious : UIButton = previousButton as! UIButton
btnPrevious.setBackgroundImage(image_off, for: .normal)
btnCurrent.setBackgroundImage(image_on, for: .normal)
if (previousButton !== sender) {
previousButton = sender
}
}

Error - EXC_BREAKPOINT (code=1, subcode=0x100308448)

error - EXC_BREAKPOINT (code=1, subcode=0x100308448)
Every time I try to double-click the divide button, Xcode issues EXC_BREAKPOINT (code = 1, subcode = 0x100308448), and my application crashes. Can you please help me solving this issue?
Dividing button - EXC_BREAKPOINT(...)
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var displayResultLabel: UILabel!
var stillTyping = false
var dotIsPlaced = false
var firstOperand: Double = 0
var secondOperand: Double = 0
var operationSign: String = ""
var currentInput: Double {
get {
return Double (displayResultLabel.text!)!
}
set {
let value = "\(newValue)"
let ValueArray = (value.components(separatedBy:"."))
if ValueArray[1] == "0" {
displayResultLabel.text = "\(ValueArray[0])"
} else {
displayResultLabel.text = "\(newValue)"
}
stillTyping = false
}
}
#IBAction func numberPressed(_ sender: UIButton) {
let number = sender.currentTitle!
if stillTyping {
if (displayResultLabel.text?.characters.count)! < 20 {
displayResultLabel.text = displayResultLabel.text! + number
}
} else {
displayResultLabel.text = number
stillTyping = true
}
}
#IBAction func twoOperandsSignPressed(sender: UIButton) {
operationSign = sender.currentTitle!
firstOperand = currentInput
stillTyping = false
dotIsPlaced = false
}
func operateWithTwoOperands(operation: (Double, Double) -> Double) {
currentInput = operation(firstOperand, secondOperand)
stillTyping = false
}
#IBAction func equalitySignPressed(sender: UIButton) {
if stillTyping {
secondOperand = currentInput
}
dotIsPlaced = false
switch operationSign {
case "+":
operateWithTwoOperands{$0 + $1}
case "-":
operateWithTwoOperands{$0 - $1}
case "✕":
operateWithTwoOperands{$0 * $1}
case "÷":
operateWithTwoOperands{$0 / $1}
default: break
}
}
#IBAction func clearButtonPressed(_ sender: UIButton) {
firstOperand = 0
secondOperand = 0
currentInput = 0
displayResultLabel.text = "0"
dotIsPlaced = false
operationSign = ""
}
// +,-
#IBAction func plusMinusButtonPressed(_ sender: UIButton) {
currentInput = -currentInput
}
#IBAction func percentageButtonPressed(_ sender: UIButton) {
if firstOperand == 0 {
currentInput = currentInput / 100
} else {
secondOperand = firstOperand * currentInput / 100
}
}
#IBAction func squareRootButtonPressed(_ sender: UIButton) {
currentInput = sqrt(currentInput)
}
#IBAction func dotButtonPressed(_ sender: UIButton) {
if stillTyping && !dotIsPlaced {
displayResultLabel.text = displayResultLabel.text! + "."
dotIsPlaced = true
} else if !stillTyping && !dotIsPlaced {
displayResultLabel.text = "0."
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
Too bad that $0 can be only Int, not Double.
You should to describe in-line functions in detail like following.
operateWithTwoOperands {first, second in return first / second;}
Thank you for reading.

How to remove "..." from my array?

In my calculator app I ran into a problem where I want ... to show in my array but only when the if statement for resultIsPending is true. Then after that I want the ... to be deleted. How can I do this in Swift? Here is the code of my ViewController.swift:
#IBOutlet weak var sequence: UILabel!
#IBOutlet weak var display: UILabel!
var userInTheMiddleOfTyping = false
var resultIsPending:Bool = false
var elements = [String]()
//var sequenceArray:Array = []
#IBAction func clear(_ sender: Any) {
display.text = " "
elements.removeAll()
elements = elements.filter{$0 != "\(String(describing: display.text))"}
sequence.text = elements.joined()
}
override func viewDidLoad() {
}
#IBAction func touchDigit(_ sender: UIButton) {
let digit = sender.currentTitle!
elements.append(digit)
combineToMakeOperationHistory()
if userInTheMiddleOfTyping{
let textCurrentlyInDisplay = display!.text!
display!.text = textCurrentlyInDisplay + digit
} else {
display!.text = digit
userInTheMiddleOfTyping = true
}
}
var displayValue: Double{
get{
return Double(display.text!)!
}
set{
display.text = String(newValue)
}
}
private var brain = CalculatorBrain()
#IBAction func performOperation(_ sender: UIButton) {
let perSender = sender.currentTitle!
elements.append(perSender)
combineToMakeOperationHistory()
if perSender == "+" || perSender == "÷" || perSender == "×" || perSender == "-" || perSender == "^"{
resultIsPending = true
}
if userInTheMiddleOfTyping{
brain.setOperand(displayValue)
userInTheMiddleOfTyping = false
}
userInTheMiddleOfTyping = false
if let mathematicalSymbol = sender.currentTitle{
brain.performOperation(mathematicalSymbol)
}
if brain.result != nil{
displayValue = brain.result!
}
}
func combineToMakeOperationHistory() {
if resultIsPending{ // this is the if statement
elements.append("...")
}else if resultIsPending == false{
}
sequence.text = elements.joined()
}
You can filter your elements array and remove the "...".
elements = elements.filter({ $0 != "..." })
Whenever you want to remove the occurrence of a String value.
you can uses something like hat
var resultIsPending:Bool = false{
didSet(isPending) {
if isPending {
elements.append("...")
} else {
elements.dropLast()
}
}
}
Don't combine data that are not of the same type. There is no reason to put ... into the array of elements:
func combineToMakeOperationHistory() {
var sequenceText: String = elements.joined()
if (resultIsPending) {
sequenceText += "..."
}
sequence.text = sequenceText
}
Since we are not appending ... to the array, we don't have to remove it.

Assigning in Swift a value but it thinks its nil

I am following the Stanford iOS Swift development course and have to make my displayValue an optional double, I assign a value to displayValue func performOperation but the getter for var displayValue: Double? reads it as nil, I have even hardcoded var displayValue: Double? to be 81 (example) but it is still read as nil, has anyone got any ideas? (I didn't want to post my whole code but if I've missed something out let me know!), thanks!
EDIT: here is my whole code now!
import UIKit
class ViewController: UIViewController {
var isThereApoint = true
var openStack = Array<Double?>()
var count = 0
#IBOutlet weak var history: UILabel!
#IBOutlet weak var calcView: UILabel!
var historyStack = Array<String>()
var userIsTyping = false
#IBAction func numberButton(sender: UIButton) {
let numberButton = sender.currentTitle!
if calcView.text!.rangeOfString("=") != nil{
calcView.text! = ""
}
if userIsTyping{
calcView.text! = calcView.text! + numberButton
}
else {
calcView.text! = numberButton
userIsTyping = true
}
}
#IBAction func decimalPoint(sender: AnyObject) {
if isThereApoint == true {
calcView.text! = calcView.text! + "."
isThereApoint = false
}
}
var displayValue: Double? {
get{
if NSNumberFormatter().numberFromString(calcView.text!) != nil{
return NSNumberFormatter().numberFromString(calcView.text!)!.doubleValue
}
else {
println("calc view in getter: \(calcView.text!)")
return 0
}
}
set{
calcView.text! = "\(newValue)"
userIsTyping = false
}
}
#IBAction func operate(sender: UIButton) {
let operation = sender.currentTitle!
switch operation{
case "×": performOperation ("×, ", {$0 * $1})
case "÷": performOperation ("÷, ",{$1 / $0})
case "+": performOperation ("+, ", {$0 + $1})
case "−": performOperation ("−, ", {$1 - $0})
case "√": performOperation ("√, ", {sqrt($0)})
case "Cos": performOperation ("Cos, ", {cos($0)})
case "Sin": performOperation("Sin, ", {sin($0)})
case "PI": displayValue! = M_PI
enter()
default: break
}
}
func performOperation(operatorSymbol: String, operation: (Double, Double) -> Double) {
if openStack.count >= 2 {
println("\(operatorSymbol)")
displayValue = operation(openStack.removeLast()!, openStack.removeLast()!)
println("display value : \(displayValue!)")
var displayAnswer = displayValue!
history.text! = history.text! + operatorSymbol
calcView.text! = "= \(displayAnswer)"
enter()
//calcView.text! = "= \(displayAnswer)"
}
}
#IBAction func positiveOrNegative(sender: AnyObject) {
if (calcView.text!.rangeOfString("-") != nil) {
calcView.text! = dropFirst(calcView.text!)
}
else {
calcView.text! = "-\(calcView.text!)"
}
}
func performOperation(operatorSymbol: String, operation: Double -> Double) {
if openStack.count >= 1 {
displayValue = operation(openStack.removeLast()!)
var displayAnswer = displayValue!
history.text! = history.text! + operatorSymbol
enter()
calcView.text! = "= \(displayAnswer)"
}
}
#IBAction func clearButton() {
history.text! = ""
for item in openStack{
openStack.removeLast()
}
calcView.text! = ""
}
#IBAction func backspace(sender: AnyObject) {
if countElements(calcView.text!) > 0 {
calcView.text! = dropLast(calcView.text!)
}
}
#IBAction func enter() {
isThereApoint = true
if history.text! == "History"{
history.text! = ""
}
openStack.append(displayValue!)
var userIsTyping = false
println("openStack = \(openStack)")
history.text! = history.text! + "\(displayValue!)" + ", "
calcView.text! = ""
}
}
Your problem is that you are setting calcView.text to "Optional(81.0)". You need to unwrap the Double? that is in newValue inside of set for displayValue:
set {
calcView.text = "\(newValue!)"
userIsTyping = false
}
Because the displayValue was "Optional(81.0)", NSNumberFormatter couldn't convert the value, so it returns nil.

Resources