Xcode Swift EXC_BAD_INSTRUCTION

时间:2016-03-19 02:43:19

标签: ios xcode swift

开发一款可以转换鞋码的应用。但是我在发布时遇到了这个错误。

  

EXC_BAD_INSTRUCTION(代码= EXC_1386_INOP,子代码= 0x0)

这是我的代码:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var mensShoeSizeTextField: UITextField!

    @IBOutlet weak var mensConvertedShoeSizeLabel: UILabel!


    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 convertButtonPressed(sender: UIButton) {
       let sizeFromTextField = mensConvertedShoeSizeLabel.text
       let numberFromTextField = Int(sizeFromTextField!)
       var integerFromTextField = numberFromTextField!
       let conversionConstant = 30
       integerFromTextField += conversionConstant
       mensConvertedShoeSizeLabel.hidden = false
       let stringWithUpdatedShoeSize = "\(integerFromTextField)"
       mensConvertedShoeSizeLabel.text = stringWithUpdatedShoeSize
    }
}

2 个答案:

答案 0 :(得分:1)

两种错误都不同。
1.关于未找到的键盘,它是警告,因为您可能会将键盘隐藏在模拟器设置中。 试试这个以冲洗警告

iOS Simulator -> Hardware -> Keyboard -> Toggle Software Keyboard
  1. 致命错误:在展开Optional值时意外发现nil 这是因为你的代码中的某个地方没有正确展开选项 我可以从你的代码中看到的一件事是你没有打开这个:

    让numberFromTextField = Int(sizeFromTextField!)

  2. 试试这样:

            let sizeFromTextField = mensConvertedShoeSizeLabel.text
            if let numberFromTextField = Int(sizeFromTextField!) {
                var integerFromTextField = numberFromTextField
                let conversionConstant = 30
                integerFromTextField += conversionConstant
                mensConvertedShoeSizeLabel.hidden = false
                let stringWithUpdatedShoeSize = "\(integerFromTextField)"
                mensConvertedShoeSizeLabel.text = stringWithUpdatedShoeSize
    
            }
    

答案 1 :(得分:0)

如果错误为unexpectedly found nil while unwrapping an Optional value,则可能是由以下某行引起的:

let numberFromTextField = Int(sizeFromTextField!)
var integerFromTextField = numberFromTextField!

您可以在此处展开​​可选值,因此如果EXC_BAD_INSTRUCTION,您将获得nil。对于将String解析为Int,如果Int(string)返回nil,如果它无法将字符串解析为数字,那么这尤其成问题。

您应该更改代码以优雅地处理nil值:

import UIKit

class ViewController: UIViewController {
    @IBOutlet var mensShoeSizeTextField: UITextField?

    @IBOutlet var mensConvertedShoeSizeLabel: UILabel?

    @IBAction func convertButtonPressed(sender: UIButton) {
        if let sizeFromTextField = mensShoeSizeTextField?.text, let mensConvertedShoeSizeLabel = mensConvertedShoeSizeLabel, let integerFromTextField = Int(sizeFromTextField) {
            let conversionConstant = 30
            let convertedIntegerFromTextField = integerFromTextField + conversionConstant
            mensConvertedShoeSizeLabel.hidden = false
            let stringWithUpdatedShoeSize = "\(convertedIntegerFromTextField)"
            mensConvertedShoeSizeLabel.text = stringWithUpdatedShoeSize
        }
    }
}