0

I got a nil error when i try to activate the "calcolatore" method. Here's the code

@IBAction func calcolatore (sender: Any) {
    let VG:Double = 80.0
    let PG = 12.0
    let Koolada = 1.0
    let orangeCream = 2.0
    let orangeMango = 2.0
    let vanilla = 2.5
    let SuperS = 0.5
    let temp = Double((porzioni.text!))
    if porzioni.text == ""{
        VGLabel.text=VGLabel.text
        PGLabel.text = PGLabel.text
        label3.text = label3.text
        label4.text = label4.text
        label5.text = label5.text
        label6.text = label6.text
        label7.text=label7.text
    }else{
        VGLabel.text = String((VG*temp!)/100.0)
        PGLabel.text = String((PG*temp!)/100.0)
        label3.text = String((Koolada*temp!)/100.0)
        label4.text = String((orangeCream*temp!)/100.0)
        label5.text = String((orangeMango*temp!)/100.0)
        label6.text = String((vanilla*temp!)/100.0)
        label7.text = String((SuperS*temp!)/100.0)

    }
}

}

and a screenshot: i can't figure it out

  • 1
    Log `porzioni.text`. I suspect it does not contain a string that is convertable to `Double`, thus `Double((porzioni.text!))` returns `nil`, thus `temp` is `nil`. – shallowThought Jan 08 '17 at 15:18
  • Don't force unwrap optionals. Use optional binding to only unwrap them if they're not `nil`: `if let temp = temp { ... }` – Keiwan Jan 08 '17 at 15:18
  • `VGLabel` is most probably a disconnected IBOutlet – Hamish Jan 08 '17 at 15:26

0 Answers0