Screenshot of simulator screen
Hello all. I just recently started trying to learn coding in Swift 2. I am trying to make a very simple app where I can add scores, push my "calculate" button and display the total in a label. I was able to do that as seen in the screenshot. The problem I have is if I don't put a score in one of the textfields and still try to calculate it, it crashes. This is my code:
class ViewController: UIViewController {
@IBOutlet weak var score1: UITextField!
@IBOutlet weak var score2: UITextField!
@IBOutlet weak var score3: UITextField!
@IBOutlet weak var total: UILabel!
@IBAction func Calculate(sender: AnyObject) {
let score1 = Int(score1.text!)!
let score2 = Int(score2.text!)!
let score3 = Int(score3.text!)!
let alltogether = (scoreone) + (scoretwo) + (scorethree)
total.text = "Your total score is \(alltogether)!"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
Could someone point me in the direction I need to look. I am thinking I need to add some sort of exemption if the field is nil. Or something along the lines of making the default int 0??? Thank you all!