1

I am making a game using swift, and I want to make a history of answers. But the problem is Xcode tells thread1 exc_bad_instruction when I click button "addResult". Here is the code. Before launching the simulator and even after launching there is no error shown, however, when I click the "addResult" button the app crashes.

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var choice1Text: UITextField!
    @IBOutlet weak var resultLabel: UILabel!
    @IBOutlet weak var choice2Text: UITextField!
    @IBOutlet var mytableView: UITableView! {
        didSet {
            mytableView.dataSource = self
        }
    }

    var items: [ResultlistItem]

    required init?(coder aDecoder: NSCoder) {
        items = [ResultlistItem]()
        let row0item = ResultlistItem()
        row0item.text = "Yes"
        items.append(row0item)
        let row1item = ResultlistItem()
        row1item.text = "No"
        items.append(row1item)
        super.init(coder: aDecoder)
    }
}
    @IBAction func addResult(sender: AnyObject) {
    let item = ResultlistItem()
        item.text = "new one"
        items.append(item)

        mytableView.reloadData()

    }



 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Resultitem", for: indexPath)

        let item = items[indexPath.row]

        configureText(for: cell, with: item)

        return cell
    }

func configureText(for cell: UITableViewCell, with item: ResultlistItem) {
    let label = cell.viewWithTag(1000) as! UILabel
    label.text = item.text


}
ar kang
  • 95
  • 1
  • 9
  • Where is your saveAnswers-Button in code? Read here why the error occurs: http://stackoverflow.com/questions/28804654/what-does-error-thread-1exc-bad-instruction-code-exc-i386-invop-subcode-0x0 Surround all non optional with the let structure. For example: if let label = cell.viewWithTag(1000) as? Label { label.text = item.text } – kuzdu Mar 26 '17 at 10:31
  • @kuzdu Thank you so much! I'll try to do it! – ar kang Mar 26 '17 at 10:33

0 Answers0