-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
71 lines (55 loc) · 1.85 KB
/
Copy pathViewController.swift
File metadata and controls
71 lines (55 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// ViewController.swift
// Calculator
//
// Created by Adele Kufour on 07/12/2017.
// Copyright © 2017 VenturesWithAd. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var display: UILabel! //automatic unwrapping
var userIsInTheMiddleOfTyping = false
@IBAction func touchDigit(_ sender: UIButton) {
let digit = sender.currentTitle! //! means unwrap this thing and give me the associated value
if userIsInTheMiddleOfTyping {
let textCurrentlyInDisplay = display.text
display.text = textCurrentlyInDisplay! + digit
} else {
display.text = digit
}
userIsInTheMiddleOfTyping = true
}
var displayValue: Double { // computed value
get {
return Double(display.text!)!
}
set{
display.text! = String(newValue)
}
}
//model always private to the controller
private var brain = CalculatorBrain()
@IBAction func performOperation(_ sender: UIButton) {
//1. if in the middle of typing a number, need to set it as an operand
if userIsInTheMiddleOfTyping{
brain.setOperand(displayValue)
userIsInTheMiddleOfTyping = false
}//end if
if let mathematicalSymbol = sender.currentTitle {
brain.performOperation(mathematicalSymbol)
}//end if let
if let result = brain.result {
displayValue = result
}
}//end method
/*
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.
}
*/
}