没有成员名称 'subscript' - SWIFT
does not have a member names 'subscript' - SWIFT
我正在构建一个问答应用程序,并且我已经制作了一个 class 用于创建问题。我还制作了一个将在 viewDidLoad() 时加载的函数。
class Question{
var quizQuestion : String = ""
var firstAnswer : String = ""
var secondAnswer : String = ""
var thirdAnswer : String = ""
var fourthAnswer : String = ""
var rightAnswer : Int = 0
init(question:String, answerOne:String, answerTwo:String, answerThree:String, answerFour:String, correctAnswer:Int)
{
self.quizQuestion = question
self.firstAnswer = answerOne
self.secondAnswer = answerTwo
self.thirdAnswer = answerThree
self.fourthAnswer = answerFour
self.rightAnswer = correctAnswer
}
}
let one = Question(question: "What was the first planet to be discovered using a telescope, in 1781?", answerOne: "Mars", answerTwo: "Jupiter", answerThree: "Uranus", answerFour: "Mercury", correctAnswer: 3)
我的 initialState() 函数
func initialState() {
var firstQuestion: AnyObject = arrayOfQuestions[0]
currentQuestion = 0
questionLabel.text = "\(firstQuestion[0])"
//Setting the buttons text
firstAnswer.setTitle("\(firstQuestion[1])", forState: UIControlState.Normal)
secondAnswer.setTitle("\(firstQuestion[2])", forState: UIControlState.Normal)
thirdAnswer.setTitle("\(firstQuestion[3])", forState: UIControlState.Normal)
fourthAnswer.setTitle("\(firstQuestion[4])", forState: UIControlState.Normal)
}
当我想将按钮文本设置为问题和答案的索引时,它说 'Question' 没有名为 'subscript' 的成员,这对我来说毫无意义。当我运行该应用程序时,所有内容都设置为 "nil."
有人可以帮忙吗?
请解释你的答案,因为我正在努力学习语言和错误。
questionLabel.text = "\(firstQuestion.quizQuestion)"
//Setting the buttons text
firstAnswer.setTitle("\(firstQuestion.firstAnswer)", forState: UIControlState.Normal)
secondAnswer.setTitle("\(firstQuestion.secondAnswer)", forState: UIControlState.Normal)
thirdAnswer.setTitle("\(firstQuestion.thirdAnswer)", forState: UIControlState.Normal)
fourthAnswer.setTitle("\(firstQuestion.fourthAnswer)", forState: UIControlState.Normal)
重组代码以使用答案数组。它会更容易管理。也给出正确答案作为数组的索引。
class Question{
var quizQuestion : String = ""
var answers : Array<String> = Array()
var rightAnswer : Int = 0
init(question:String, answerOne:String, answerTwo:String, answerThree:String, answerFour:String, correctAnswer:Int)
{
self.quizQuestion = question
self.answers.append(answerOne)
self.answers.append(answerTwo)
self.answers.append(answerThree)
self.answers.append(answerFour)
self.rightAnswer = correctAnswer
}
}
let one = Question(question: "What was the first planet to be discovered using a telescope, in 1781?",
answerOne: "Mars", answerTwo: "Jupiter", answerThree: "Uranus", answerFour: "Mercury", correctAnswer: 3)
func initialState() {
var firstQuestion: Question = arrayOfQuestions[0]
questionLabel.text = "\(firstQuestion.quizQuestion)"
//Setting the buttons text
firstAnswer.setTitle("\(firstQuestion.answers[0])", forState: UIControlState.Normal)
secondAnswer.setTitle("\(firstQuestion.answers[1])", forState: UIControlState.Normal)
thirdAnswer.setTitle("\(firstQuestion.answers[2])", forState: UIControlState.Normal)
fourthAnswer.setTitle("\(firstQuestion.answers[3])", forState: UIControlState.Normal)
}
我正在构建一个问答应用程序,并且我已经制作了一个 class 用于创建问题。我还制作了一个将在 viewDidLoad() 时加载的函数。
class Question{
var quizQuestion : String = ""
var firstAnswer : String = ""
var secondAnswer : String = ""
var thirdAnswer : String = ""
var fourthAnswer : String = ""
var rightAnswer : Int = 0
init(question:String, answerOne:String, answerTwo:String, answerThree:String, answerFour:String, correctAnswer:Int)
{
self.quizQuestion = question
self.firstAnswer = answerOne
self.secondAnswer = answerTwo
self.thirdAnswer = answerThree
self.fourthAnswer = answerFour
self.rightAnswer = correctAnswer
}
}
let one = Question(question: "What was the first planet to be discovered using a telescope, in 1781?", answerOne: "Mars", answerTwo: "Jupiter", answerThree: "Uranus", answerFour: "Mercury", correctAnswer: 3)
我的 initialState() 函数
func initialState() {
var firstQuestion: AnyObject = arrayOfQuestions[0]
currentQuestion = 0
questionLabel.text = "\(firstQuestion[0])"
//Setting the buttons text
firstAnswer.setTitle("\(firstQuestion[1])", forState: UIControlState.Normal)
secondAnswer.setTitle("\(firstQuestion[2])", forState: UIControlState.Normal)
thirdAnswer.setTitle("\(firstQuestion[3])", forState: UIControlState.Normal)
fourthAnswer.setTitle("\(firstQuestion[4])", forState: UIControlState.Normal)
}
当我想将按钮文本设置为问题和答案的索引时,它说 'Question' 没有名为 'subscript' 的成员,这对我来说毫无意义。当我运行该应用程序时,所有内容都设置为 "nil."
有人可以帮忙吗? 请解释你的答案,因为我正在努力学习语言和错误。
questionLabel.text = "\(firstQuestion.quizQuestion)"
//Setting the buttons text
firstAnswer.setTitle("\(firstQuestion.firstAnswer)", forState: UIControlState.Normal)
secondAnswer.setTitle("\(firstQuestion.secondAnswer)", forState: UIControlState.Normal)
thirdAnswer.setTitle("\(firstQuestion.thirdAnswer)", forState: UIControlState.Normal)
fourthAnswer.setTitle("\(firstQuestion.fourthAnswer)", forState: UIControlState.Normal)
重组代码以使用答案数组。它会更容易管理。也给出正确答案作为数组的索引。
class Question{
var quizQuestion : String = ""
var answers : Array<String> = Array()
var rightAnswer : Int = 0
init(question:String, answerOne:String, answerTwo:String, answerThree:String, answerFour:String, correctAnswer:Int)
{
self.quizQuestion = question
self.answers.append(answerOne)
self.answers.append(answerTwo)
self.answers.append(answerThree)
self.answers.append(answerFour)
self.rightAnswer = correctAnswer
}
}
let one = Question(question: "What was the first planet to be discovered using a telescope, in 1781?",
answerOne: "Mars", answerTwo: "Jupiter", answerThree: "Uranus", answerFour: "Mercury", correctAnswer: 3)
func initialState() {
var firstQuestion: Question = arrayOfQuestions[0]
questionLabel.text = "\(firstQuestion.quizQuestion)"
//Setting the buttons text
firstAnswer.setTitle("\(firstQuestion.answers[0])", forState: UIControlState.Normal)
secondAnswer.setTitle("\(firstQuestion.answers[1])", forState: UIControlState.Normal)
thirdAnswer.setTitle("\(firstQuestion.answers[2])", forState: UIControlState.Normal)
fourthAnswer.setTitle("\(firstQuestion.answers[3])", forState: UIControlState.Normal)
}