使用未解析的标识符“questionList”
Use of unresolved identifier 'questionList"
我知道这个错误意味着编译器无法识别该名称,但我不确定如何修复它。我将如何以不同方式设置变量以使其正常运行?我假设它必须做一些事情,questionsList 是一个 var 而不是 class?或者它可能需要包含在该行的其他地方?
var score = 0
for question in questionsList {
score += question.selectedAnswerIndex!**
(下面的完整代码:我删除了中间的一些代码以将其清理到 post,但这应该不是问题。我还在问题所在的位置加注星标出现)
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)!"
}
用 static
关键字这样声明
class QuestionController: UITableViewController {
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)]
访问使用
QuestionController.questionsList
如果问题是硬编码的,请将它们移动到 Question
结构中,如 class 属性
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)]
}
并从任何地方调用它
Question.questionsList
我知道这个错误意味着编译器无法识别该名称,但我不确定如何修复它。我将如何以不同方式设置变量以使其正常运行?我假设它必须做一些事情,questionsList 是一个 var 而不是 class?或者它可能需要包含在该行的其他地方?
var score = 0
for question in questionsList {
score += question.selectedAnswerIndex!**
(下面的完整代码:我删除了中间的一些代码以将其清理到 post,但这应该不是问题。我还在问题所在的位置加注星标出现)
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)!"
}
用 static
关键字这样声明
class QuestionController: UITableViewController {
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)]
访问使用
QuestionController.questionsList
如果问题是硬编码的,请将它们移动到 Question
结构中,如 class 属性
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)]
}
并从任何地方调用它
Question.questionsList