Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

At this point in the quiz, the app is supposed to calculate the final score. At the moment, it crashes after selecting an answer on the final question, with the title error appearing on the third line:

var score = 0
for question in QuestionController.questionsList {
    score += question.selectedAnswerIndex!

Full code for that swift file

class ResultsController: UIViewController {

  let resultsLabel: UILabel = {
    let label = UILabel()
    label.text = "Congratulations! You'd make a great Ross!"
    label.contentMode = .scaleToFill
    label.numberOfLines = 0
    label.translatesAutoresizingMaskIntoConstraints = false
    label.textAlignment = .center
    label.font = UIFont.boldSystemFont(ofSize: 30)
    return label
  }()

  override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(done(sender:)))

    navigationItem.title = "Results"

    view.backgroundColor = UIColor.white

    view.addSubview(resultsLabel)
    view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": resultsLabel]))
    view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": resultsLabel]))

    let names = ["Ross", "Joey", "Chandler", "Monica", "Rachel", "Phoebe"]

    var score = 0
    for question in QuestionController.questionsList {
      score += question.selectedAnswerIndex!
    }

    let result = names[score % names.count]
    resultsLabel.text = "Congratulations! \(result)."
  }

  @objc func done(sender: UIBarButtonItem) {

    navigationController?.popToRootViewController(animated: true)

  }

}
Rich Donnellan
Rich Donnellan
Treehouse Moderator 27,671 Points

Question updated with code formatting. Check out the Markdown Cheatsheet link below the "Add an Answer" for syntax examples.

1 Answer

Addison Francisco
Addison Francisco
9,561 Points

Emily,

Which challenge is this for?