我正在制作一个简单的琐事应用程序,以帮助学习语言 . 目前我正在研究UIButton的下一个问题 . 我不知道我做错了什么 .
func nextQuestion() {
currentQuestion += 1
var newQuestion: AnyObject = arrayOfQuestions[currentQuestion]
questionCorrectAnswer = newQuestion[9].integerValue
firstAnswer.hidden = false
secondAnswer.hidden = false
thirdAnswer.hidden = false
fourthAnswer.hidden = false
firstAnswer.setTitle("\(newQuestion[1])", forState: UIControlState.Normal)
secondAnswer.setTitle("\(newQuestion[2])", forState: UIControlState.Normal)
thirdAnswer.setTitle("\(newQuestion[3])", forState: UIControlState.Normal)
fourthAnswer.setTitle("\(newQuestion[4])", forState: UIControlState.Normal)
questionLabel.text = "\(newQuestion[0])"
myNextQuestion.hidden = true
}
编辑:
var newQuestion : AnyObject = arrayOfQuestions[currentQuestion]
我的newQuestion变量设置为我的问题数组 .
let firstQuestion :AnyObject = 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)
let secondQuestion = Question(question: "Who averaged 1 patent for every three weeks of his life?", answerOne: "Ben Franklin", answerTwo: "Thomas Edison", answerThree: "Henry Ford", answerFour: "Ezra Gilliland", correctAnswer: 2)
let thirdQuestion = Question(question: "Which island is the world's largest island?", answerOne: "Iceland", answerTwo: "Australia", answerThree: "Hawaii", answerFour: "Greenland", correctAnswer: 4)
let fourthQuestion = Question(question: "What is the diameter of the Earth?", answerOne: "5,000 Miles", answerTwo: "6,000 Miles", answerThree: "8,000 Miles", answerFour: "10,000 Miles", correctAnswer: 3)
let fifthQuestion = Question(question: "The US is the world's 5th largest producer of potatoes. What are the two top potato producing countries?", answerOne: "Canada/Italy", answerTwo: "China/Russa", answerThree: "China/Spain", answerFour: "Hawaii/Russia", correctAnswer: 2)
let sixthQuestion = Question(question: "What is the symbol for iron on the periodic table?", answerOne: "Fe", answerTwo: "Ne", answerThree: "Se", answerFour: "Io", correctAnswer: 1)
let seventhQuestion = Question(question: "The Hubble Telescope is named after which astronomer?", answerOne: "Frank Hubble", answerTwo: "Timothy Hubble", answerThree: "Edwin Hubble", answerFour: "Roger Hubble", correctAnswer: 3)
let eighthQuestion = Question(question: "What year did the Apple's first iPhone become available?", answerOne: "2007", answerTwo: "2005", answerThree: "2005", answerFour: "2003", correctAnswer: 1)
let ninethQuestion = Question(question: "OS computer abbreviation usually means what?", answerOne: "Optical Sensor", answerTwo: "Operating System", answerThree: "Open Software", answerFour: "Operating Sensor", correctAnswer: 2)
let tenthQuestion = Question(question: "Where was the first mouse designed?", answerOne: "Apple", answerTwo: "Microsoft", answerThree: "Xerox", answerFour: "Hewlett-Packard", correctAnswer: 3)
//Array of Questions Set
arrayOfQuestions = [firstQuestion, secondQuestion, thirdQuestion, fourthQuestion, fifthQuestion, sixthQuestion, seventhQuestion, eighthQuestion, ninethQuestion, tenthQuestion]
当我在手机上运行应用程序时,我回答了问题,然后点击了下一个问题,应用程序崩溃了 . 我真的不知道这个错误告诉了我什么 . 请解释一下你的答案!
2 回答
确保
newQuestion
数组不是nil
并且索引10的值为非零值 .如果我在这里提到的内容已经完成,请随意使用此数组的内容更新您的问题 .
Edit
现在我可以看到更多代码了,这很明显:
你说
newQuestion[9].integerValue
但你应该使用newQuestion[5].integerValue
. 您要查找的整数值位于索引5,而不是9 .我可能错了,但以下几行似乎是错误的:
你声明
newQuestion
是AnyObject类型,它是 not 一个数组类型,然后你在下一行中取消引用它就好像它是一个数组!这会触发致命错误!我建议您将
arrayOfQuestions
的声明更改为var arrayOfQuestions : Array<Question>()
之类的内容,并考虑使用for循环来实现nextQuestion()
的其他一些功能 . 也许类似于以下内容: