Created
November 1, 2014 18:26
-
-
Save simonboak/6ba53ba98639ba618f3a to your computer and use it in GitHub Desktop.
ioscreator.com - iOS 8 Calculator updated view controller
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
import UIKit | |
class ViewController: UIViewController { | |
var isTypingNumber = false | |
var firstNumber:Int! | |
var secondNumber:Int! | |
var operation = "" | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
firstNumber = 0 | |
secondNumber = 0 | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
@IBOutlet weak var calculatorDisplay: UILabel! | |
@IBAction func numberTapped(sender: AnyObject) { | |
var number = sender.currentTitle | |
if isTypingNumber { | |
calculatorDisplay.text = calculatorDisplay.text! + number!! | |
} else { | |
calculatorDisplay.text = number | |
isTypingNumber = true | |
} | |
} | |
@IBAction func calculationTapped(sender: AnyObject) { | |
isTypingNumber = false | |
firstNumber = calculatorDisplay.text?.toInt()! | |
operation = sender.currentTitle!! | |
} | |
@IBAction func equalsTapped(sender: AnyObject) { | |
isTypingNumber = false | |
var result = 0 | |
secondNumber = calculatorDisplay.text?.toInt()! | |
if operation == "+" { | |
result = firstNumber + secondNumber | |
} else if operation == "-" { | |
result = firstNumber - secondNumber | |
} | |
calculatorDisplay.text = "\(result)" | |
} | |
} |
calculatorDisplay.text?.toInt()!
becomes :
Int(calculatorDisplay.text!)!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
try to update the code, please! There's no .toInt() function in Swift anymore! This code it won't work like it's written!