小费计算器 - 意外地发现零,同时展开一个可选值

问题描述:

我遇到了我的应用程序的问题。我试图在按一个按钮后在文本框中输入的内容的15%显示在标签中。这是我到目前为止的代码:小费计算器 - 意外地发现零,同时展开一个可选值

@IBAction func calculateButton(sender: UIButton) 
{ 
    var fifteenPercent: Double 
    fifteenPercent = 0.15 
    var billTop = billTextField.text.toInt()! 
    var billTipped: Double 
    billTipped = Double(billTop) * fifteenPercent 

    tipAmountLabel.text = "$\(billTipped)" 

当我启动应用程序,并按下按钮,我得到的错误

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

+0

可我知道什么是您的Xcode版本 –

+0

的Xcode版本:版本6.4(6E35b) – ABlach

它看起来像您的问题展开的字符串为int,尝试获得这样说:

var billTop = (billTextField.text as NSString).doubleValue 
+0

谢谢!这是解决方案 – ABlach

+0

不是问题:) – Swinny89

闻起来像你billTextField没有它的IBOutlet中迷上了。您正在迫使billTextField中的text的拆封。如果这是零,它会炸毁。

+0

不幸的是它,现在我真的很困惑 – ABlach

添加这样的:

var billTop = Int(txt_address.text!) 
tipAmountLabel.text = String(format:"$%.2f", billTipped) 
+0

这有帮助,但这出现http://imgur.com/llNwbZB – ABlach

+0

请参阅编辑请 – iAnurag