2015-10-20 67 views
0

我遇到了我的應用程序的問題。我試圖在按一個按鈕後在文本框中輸入的內容的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

回答

0

它看起來像您的問題展開的字符串爲int,嘗試獲得這樣說:

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

謝謝!這是解決方案 – ABlach

+0

不是問題:) – Swinny89

0

聞起來像你billTextField沒有它的IBOutlet中迷上了。您正在迫使billTextField中的text的拆封。如果這是零,它會炸燬。

+0

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

0

添加這樣的:

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

這有幫助,但這出現http://imgur.com/llNwbZB – ABlach

+0

請參閱編輯請 – iAnurag

相關問題