Swift中的EXC_BAD_INSTRUCTION

时间:2014-10-29 17:32:07

标签: xcode swift

我是Swift的新手。所以我做了这个教程来制作小费计算器,它工作正常。我开始了一个新项目,看看我是否可以重新创建它,并保留原始代码作为参考。我以完全相同的方式创建我的应用程序,但使用不同的变量名称。

我在lblABVCalculated.text行收到此错误:

  

主题1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)

为什么以下代码不起作用的任何想法?

标题

import UIKit

class FirstViewController: UIViewController {

    @IBOutlet weak var txtOG: UITextField!
    @IBOutlet weak var txtFG: UITextField!
    @IBOutlet weak var lblABVCalculated: UILabel!

    var OG = ""
    var FG = ""
    var answer : Float = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func btnCalculate(sender: UIButton) {
        calculateABV()
    }
    @IBAction func btnClear(sender: UIButton) {
        txtOG.text = ""
        txtFG.text = ""

        lblABVCalculated.text = "ABV"
    }

    func calculateABV() -> Bool {
        OG = txtOG.text
        FG = txtFG.text

        var fOG = (OG as NSString).floatValue
        var fFG = (FG as NSString).floatValue

        answer = fOG * (fFG * 0.01)

        var answerFormat : NSString = NSString(format: "%0.2f", answer)

        lblABVCalculated.text = "ABV: \(answerFormat)"
        // Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

        return true
    }
}

0 个答案:

没有答案