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
}