I know this error means that the compiler doesn't recognize the name, but I'm not sure how to fix it. How would I set the variable differently in order for it to function correctly? I assume it has to do something with questionsList being a var as opposed to a class? Or maybe it needs to be included somewhere else in the line?
var score = 0
for question in questionsList {
score += question.selectedAnswerIndex!**
(Full code below: I've deleted some of the code in the middle in order to clean this up to post, but it shouldn't be an issue. I also starred where the issue is appearing)
import UIKit
struct Question {
var questionString: String?
var answers: [String]?
var selectedAnswerIndex: Int?
}
class QuestionController: UITableViewController {
let cellId = "cellId"
let headerId = "headerId"
var questionsList: [Question] = [Question(questionString: "What is your favorite type of food?", answers: ["Sandwiches", "Pizza", "Seafood", "Unagi"], selectedAnswerIndex: nil), Question(questionString: "What do you do for a living?", answers: ["Paleontologist", "Actor", "Chef", "Waitress"], selectedAnswerIndex: nil), Question(questionString: "Were you on a break?", answers: ["Yes", "No"], selectedAnswerIndex: nil)]
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Question"
navigationController?.navigationBar.tintColor = UIColor.white
navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
tableView.register(AnswerCell.self, forCellReuseIdentifier: cellId)
tableView.register(QuestionHeader.self, forHeaderFooterViewReuseIdentifier: headerId)
tableView.sectionHeaderHeight = 50
tableView.tableFooterView = UIView()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let index = navigationController?.viewControllers.index(of: self) {
let question = questionsList[index]
if let count = question.answers?.count {
return count
}
}
return 0
....
class ResultsController: UIViewController {
let resultsLabel: UILabel = {
let label = UILabel()
label.text = "Congratulations, you'd make a great Ross!"
label.translatesAutoresizingMaskIntoConstraints = false
label.textAlignment = .center
label.font = UIFont.boldSystemFont(ofSize: 14)
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .plain, target: self, action: Selector(("done")))
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 questionsList {
score += question.selectedAnswerIndex!**
}
let result = names[score % names.count]
resultsLabel.text = "Congratulations, you'd make a great \(result)!"
}
If the questions are hard-coded move them into the Question
struct as class property
struct Question {
let questionString: String
let answers: [String]
var selectedAnswerIndex: Int?
static let questionsList : [Question] = [Question(questionString: "What is your favorite type of food?", answers: ["Sandwiches", "Pizza", "Seafood", "Unagi"], selectedAnswerIndex: nil), Question(questionString: "What do you do for a living?", answers: ["Paleontologist", "Actor", "Chef", "Waitress"], selectedAnswerIndex: nil), Question(questionString: "Were you on a break?", answers: ["Yes", "No"], selectedAnswerIndex: nil)]
}
and call it from everywhere
Question.questionsList